Trouble Controlling Excel with Word using VBA; late binding and early binding aren't working

1.1k views Asked by At

I am having trouble using the automation to control Excel from Word that should be possible from the GetObject function.

I am using Office 2011 for Mac. My goal is take information from a word document and insert it into an excel workbook after the most recent such entry. The word doc is a contract generator. Each workbook will contain the details for about 30 related contracts. The logic I would like to use is to allow the user to enter the name for the workbook and then VBA will get the full path name and path and will then be able to control the workbook. However, I cannot make a simpler case work.

To start, I cannot even get word to control a currently existing instance of excel (though CreateObject("Excel.Application") does work. I tried early binding and got an:

automation error (91)

Here is the code that produces that:

Option Explicit

Sub WorkOnAWorkbook()

   Dim Oxl As Excel.Application
   Dim owB As Excel.Workbook

   Set Oxl = GetObject(, "Excel.Application")
   Set owB = Oxl.Workbooks.Open(fileName:="Macintosh HD:Users:User:Desktop:Test.xlsx")

End Sub

I also tried using late binding and that gives me:

error 424 (object required)

Here is the code that produces that:

Option Explicit

Sub WorkOnAWorkbook()

    Dim Oxl As object
    Dim owB As object

    Set Oxl = GetObject(, "Excel.Application")
    Set owB = Oxl.Workbooks.Open(fileName:="Macintosh HD:Users:User:Desktop:Test.xlsx")

End Sub

I went to tools->references and added in Excel objects so I am not sure this simple part of the program won’t even work.

I would love if someone can help me to get this part working and, ideally, the part of my program that will allow me to collect the name of the workbook (via an input box, get the fullpath name of the inputted file and then control that workbook. Everything else works in my program, but I cannot get this key bridge between word and excel to function.

1

There are 1 answers

1
D_Bester On BEST ANSWER

Try:

Set owB = Oxl.Workbooks.Open(FileName:="\Macintosh HD\Users\User\Desktop\Test.xlsx")

Or if that doesn't work, try:

Dim myFileName As String
myFileName = Application.GetOpenFilename
Debug.Print myFileName

If myFileName <> "" Then
    Set owB = Oxl.Workbooks.Open(FileName:=myFileName)
End If

Also for early binding you don't need GetObject if you use New keyword in your Dim statement:

Sub WorkOnAWorkbook()

   Dim Oxl As New Excel.Application
   Dim owB As Excel.Workbook

   'Not needed Set Oxl = GetObject(, "Excel.Application")
   Set owB ...

End Sub