Overwrite when paste to powerpoint VBA - vba

If i have texts(shapes) in my powerpoint. How can i modify my code NOT to create a new shape but overwrite the value inside it.
The code below creates a new shape.
Any Ideas?
Private Sub test()
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlWorkBook = xlApp.Workbooks.Open("D:\ELE_powerpoint\book1.xlsx", True, False)
With xlWorkBook.ActiveSheet
xlWorkBook.sheets(1).Range("A2").Copy
End With
ActivePresentation.Slides(1).Shapes.Paste.Select
Set xlApp = Nothing
Set xlWorkBook = Nothing
End Sub

lets use a variable instead of copying and pasting like this
mytext = xlWorkBook.sheets(1).Range("A2")
you can then set the text value of a shape like this
ActivePresentation.Slides(1).Shapes("Shape name").TextFrame.Characters.Text = myText
read about how to set the name of a shape here: How to name an object within a PowerPoint slide?

You need to find the index of the shape you want to paste into. Say it is index 2
Then use
ActivePresentation.Slides(1).Shapes(2).TextFrame.TextRange.Paste

Related

Loading Powerpoint Array from Excel with VBA

I am trying to have powerpoint macro populate an array by taking data from excel file. In fact, there are two arrays. Same code, same file, but one array gets populated and the other one doesn't (or gets only partially populated). Below, FindList array is working fine and ReplaceList does not. I'd appreciate any help or suggestions for an alternative method.
Option Explicit
Sub Multi_FindReplace()
Dim FindList As Variant
Dim ReplaceList As Variant
Dim x, i, j As Long
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Users\wip\dev\DRAFT Exhibits (Client) 04-06-18.xlsm")
xlBook.Application.Visible = False
FindList = xlBook.Worksheets("FindReplace").Range("A1:A22").Value
ReplaceList = xlBook.Worksheets("FindReplace").Range("B1:B22").Value
Problem solved with using a single array to load all the data. Looks like this:
ExcelData = xlBook.Worksheets("FindReplace").Range("A1:B22").value

Loading data in Powerpoint array with VBA

I am trying to load data in Powerpoint VBA array from Excel. This data will be used later to perform find/replace in powerpoint. I get stuck at the last line of the code below. I accepted this code to load the array for future operations:
Dim FindList As Variant '<- this is array to be loaded with data
Dim xlApp As Object
Dim xlBook As Object
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Users\Boxync\findreplace.xlsx")
Set FindList = xlBook.Worksheets("Sheet1").Range("A1:A4").Value
You do not need Set. If you delete it, your code should work.
To make it a bit better, you can use Application.Transpose():
With xlBook.Worksheets("Sheet1")
FindList = Application.Transpose(.Range("A1:A4").Value2)
End With
Then access the array like this - FindList(1), FindList(2).

How Do I Use the Excel Function LoadPicture() from a Windows Form Application

I have a Windows Form application that my company uses to access all it's reports. Most of the reports are given to the user in an Excel sheet that is created at run-time either from scratch or an Excel Template. This has been working fine for everything up until now. The problem I am running into now is that I need to load an ImageBox on the Excel Template with an image saved on the drive. I have the filepath of the image (this will change each time this run). The only way I have found to be able to set the picture property of the ImageBox is like this...
Dim FileStr As String = "C:\Folder\ImageFile.jpg"
xlWorksheet.ImageName.Picture = LoadPicture(FileStr)
The problem is I can't figure out how to call the LoadPicture() function from within the windows form. I know I could create an Excel Module at run-time that call the LoadPicture() then delete it, but i just figured there had to be a better way? Hoping someone out there has suggestions. Thanks.
Edit:- Here is an example of the code I am Using to Open The Excel Sheet
Imports ExcelVB = Microsoft.Office.Interop.Excel
Imports ad = GartnerInterface.AdminClass.AdminTools
Imports xl = GartnerInterface.AdminClass.XlHelp
Public Class TestClass
Public Shared Sub NewSub()
Dim xlApp As ExcelVB.Application
Dim xlWorkbook As ExcelVB.Workbook
Dim xlWorksheet As ExcelVB.Worksheet
Dim TestSht As String
TestSht = "H:\Josh\ExcelTest.xlsm"
xlApp = CreateObject("Excel.Application")
xlWorkbook = xlApp.Workbooks.Add(TestSht)
xlApp.DisplayAlerts = False
xlApp.Visible = True
xlWorksheet = xlApp.Sheets("Sheet1")
Dim FileStr As String = "H:\12117\12117_Original.png"
'xlWorksheet.RFQImg.Picture = LoadPicture(FileStr)
End Sub
End Class
Now that I'm looking at the code, you would likely need the .AddPicture method
At the bottom of this code, you would need something like the following:
Dim FileStr As String = "H:\12117\12117_Original.png"
xlWorksheet.Shapes.AddPicture(FileStr, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45)
Taken from,
https://msdn.microsoft.com/en-us/library/office/ff198302.aspx
OR, if you have a template where the image is already named "test"
Dim FileStr As String = "H:\12117\12117_Original.png"
Dim imgName as String = "test"
For Each myShape In xlWorksheet.Shapes
If myShape.Name = imgName then
cTop = myShape.ShapeRange.Top 'we must save the values here
cLeft = myShape.ShapeRange.Left
cHeight = myShape.ShapeRange.Height
cWidth = myShape.ShapeRange.Width
myShape.delete
Exit For
end if
next
xlWorksheet.Shapes.AddPicture(FileStr, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, cLeft, cTop, cWidth, cHeight)
An example, with user interaction, I borrowed from

Visio button to compare text in excel file; error 438

I am trying to make a visio diagram that you click a button and it searchs and excel file for the "location" once it find the location in the excel file it then copies over a URL and proceeds to open that URL with the default browser. I keep getting runtime error 438: Object doesn't support this property or method. Any ideas?
Option Compare Text
Private Sub Mail_Room_Click()
Dim XLApp As Excel.Application
Dim XLWB As Excel.Workbook
Set XLApp = New Excel.Application
Set XLWB = XLApp.Workbook.Open("C:\printers\schprint.xlsx")
Set XLWsht = XLWB.Sheets(1)
Dim URL As String
Dim Location As String
Location = "Mail Room"
URL = ""
For Each i In XLWsht.Range("D2:D11")
If StrComp(i.Cells.Value, Location) = 0 Then
URL = i.Cells.Offset(7, 0).Value
Exit For
End If
Next i
CreateObject("WScript.Shell").Run (URL)
End Sub
I think you're just missing as 's' in XLApp.WorkbookS.Open("C:\printers\schprint.xlsx")
That should fix the problem.

Open Excel file in VBA from Powerpoint

I'm trying to open the Excel file using VBA in Powerpoint 2010 with the help of following code.
Private Sub CommandButton1_Click()
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "C:\lol\Book1.xlsx", True, False
Set xlApp = Nothing
Range("A8").Value = "Hello"
End
But I'm getting the following error.
Compile Error
User Defined type not defined.
Am I missing something. Can anyone share the sample piece of code to open an excel file, change a cell value and close Excel file in Powerpoint 2007 and 2010 using VBA.
I have searched a lot and tried different pieces of code, but getting the same error everytime. :(
Thanks in advance. :)
Have you added a reference to the Excel Object Model? That would save you having to use the late bound objects (and you get the benefit of having the Intellisense help when you are coding).
You need to go to Tools -> References and check the "Microsoft Excel v.x Object Library" (I think that number changes depending on the version of office you are using.
Your code should work if you do that, you should also remove the
CreateObject("Excel.Application")
line and replace it with
Set xlApp = new Excel.Application
And move the
Set xlApp = nothing
line to the end of your subroutine.
The rest of your code looks fine to me.
Late binding code would be this
Private Sub test()
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkbook = xlApp.Workbooks.Open("C:\lol\Book1.xlsx", True, False)
xlWorkbook.sheets(1).Range("A8").Value = "Hello"
Set xlApp = Nothing
Set xlWorkbook = Nothing
End Sub
It's better to use early binding though.