Loading data in Powerpoint array with VBA - 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).

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

Overwrite when paste to powerpoint 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

Set xWorkb = New workbook not working, ActiveX error

I have some code below, It works like a charm but I'm curious about some things.
Why can't I make a Set xWorkb = new Workbook statement? Instead I use the Dim xWorkb as new Workbook, which works. But I've learned (hopefully correct) that using the new statement within a Dim is bad practice, and that you should create the object seperately. So why doesn't it work? I get a ActiveX component can't create object error, but the xWorkb is still being created later as an object right due to the new statement in the Dim section? Makes me confusing.
Why can't I use the excel.application.workbooks when defining variable xApp? Is it because I have to specify a workbook and can't just leave the workbooks empty like that? I get a type mismatch error when I'm trying to change excel.application to excel.application.workbooks.
Sub tester()
Dim xWorkb As New Workbook
Dim xApp As Excel.Application: Set xApp = New Excel.Application
Dim xFiles_target() As Variant
Dim file_path As String
xFiles_target = Array("Bella.xls", "Fizz.xls", "Milo.xls", "Jake.xls")
file_path = Dir("C:\Users\hans\Desktop\")
Do While Len(file_path) > 0
Debug.Print file_path
If UBound(Filter(xFiles_target, file_path)) >= 0 Then
Debug.Print "found " & file_path
Set xWorkb = xApp.Workbooks.Open("C:\Users\hans\Desktop\" & file_path)
xApp.ActiveSheet.Cells(2, 2) = "tester"
xWorkb.Save
xWorkb.Close
End If
file_path = Dir
Loop
End Sub
You cannot create new workbooks with New because workbooks are coupled with Application and must be created with Workbooks.Add or Workbooks.Open.
Dim xWorkb as new Workbook does not work - it appears to work because you don't access xWorkb between declaring it and assigning it with Workbooks.Open. If you did, you would get the same ActiveX component can't create object error.
The error is because Excel.Workbook does not have any public constructors.
You cannot define a variable as excel.application.workbooks because that is not a type. It is a property named Workbooks, of type Excel.Workbooks, that belongs to an object named Application of type Excel.Application.
You can declare the variable as Excel.Workbooks, but you probably don't want to, because you will need to create an Excel.Application to use it anyway.

VBA: Reading excel data into word

I am making a simple form that extract the data from my excel sheet, such as name, date of birth, and address. And inserting them into my word form, I am doing 20-30 sheets everytime, so I think it might be able to save the copying & pasting time.
I tried to follow this tutorial: http://www.makeuseof.com/tag/integrate-excel-data-word-document/
And created a button with a simple label named m_name, for member's name. But it tells me Compile error: User-defined type not defined. And flaged on line 1.
I am using Word 2003, (I am not able to find the Tools > Reference as the guide was asking for). I am not sure if it is related to this error.
Private Sub CommandButton1_Click()
Dim objExcel As Excel.Application
Dim exWb As Excel.Workbook
Set exWb = objExcel.Workbooks.Open("U:\test.xls")
ThisDocument.m_name.Caption = exWb.Sheets("Member's Data").Cells(3, 3)
exWb.Close
Set exWb = Nothing
End Sub
Yes, it's very important to set references according to the tutorial.
However, change these two lines:
Dim objExcel As Excel.Application
Dim exWb As Excel.Workbook
to:
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Application")
and the code should work, too.

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.