Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening MS Word Application in Excel VBA

Status
Not open for further replies.

Dawber

Technical User
Jun 29, 2001
86
0
0
GB
I am having problems attempting to insert data, gathered from an Excel 2003 SP3 workbook, into an MS Word document.

The macro gathers the information satisfactorily but fails on the first line of the following code, which results in a "Compile error: User defined type not defined", with the first line highlighted.

[code/]Dim appWord As Word.Application
Dim aDoc As Word.Document
Set appWord = CreateObject("Word.Application")
Set aDoc = appWord.Documents.Open(Filename:="c:\test.doc")
aDoc.Application.Visible = True
aDoc.Bookmarks("A").Range.Text = text_a
aDoc.Bookmarks("B").Range.Text = text_b
aDoc.SaveAs (Filename:="c:\" & text_a & ".doc")
aDoc.Close
Set aDoc = Nothing
appWord.Quit
Set appWord = Nothing [/code]
 
It appears you need to add a reference to MS Word:
Tools > References... > Select MS Word 12.0 Object Library
 
Thanks for taking the time to educate me Dave, I'm using someone else's PC and never thought of that.
 
It appears you need to add a reference to MS Word:
Tools > References... > Select MS Word 12.0 Object Library
If that is indeed the appropriate library for the version of Word you are making reference to. Our organization uses Office 2002 (XP), so the library is MS Word 10.0 Object Library. This is early binding.

Otherwise, you can use late-binding. In which case, your code has to change a bit.

If you know what version of Word you are working with, and you know that other versions will not be used, generally speaking, early-binding is "better".

The difference is early-binding is version specific. Late-binding is not.



"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top