I have a VBA application written in Excel that opens Word documents and manipulates them.
I believe I want to use Late Binding because:
- I don't want the user to have to add the reference library
- I don't know what version of Excel / Word they are on anyway.
So to use late binding, I understand you must first declare the object "as Object":
Dim oWordApp As Object
Dim oActiveDoc As Object
Dim oSection As Object
Dim oSections As Object
Dim oTable As Object
Dim oTables As Object
Then, in the procedure, create the objects with the appropriate library reference:
Set oWordApp = CreateObject("Word.Application")
Set oActiveDoc = CreateObject("Word.Document")
Set oSection = CreateObject("Word.Section")
Set oSections = CreateObject("Word.Sections")
Set oTable = CreateObject("Word.Table")
Set oTables = CreateObject("Word.Tables")
This approach works for oWordApp and oActiveDoc objects. It fails at oSection and beyond.
When I'm able to trap an error, it's a 429 error.
Any ideas? (By the way, there more Word objects needed, just not shown for brevity. Also, I need to extend this approach to Visio objects used within Excel VBA once I figure it out.
Thanks
CodePudding user response:
You only need CreateObject
for the Application. Once you've created an instance of Word, then use oWordApp
to create new documents, manipulate tables and sections, and so on.
Set oWordApp = CreateObject("Word.Application")
Set oActiveDoc = oWordApp.Documents.Add()
and so on.