Error opening Word document from Excel VBA code

E

engineer_rich

I am trying to upgrade from Office 2003 to Office 2007. One problem I am
having is opening a Word document from Excel VBA code. It works fine in 2003
version but not in 2007 version.
Here's the code:
-----
Sub OpenMyDocument()
Dim myDoc As String
myDoc = [full path name of my document in quotes]

Dim myDocument As Document

Set myDocument = Documents.Open(myDoc)
MsgBox "opened"
myDocument.Close
MsgBox "closed"

Set myDocument = Nothing
End Sub
-----

I added a reference to "Microsoft Word 11.0 Object Library" for the Excel
2003 version which automatically becomes "Microsoft Word 12.0 Object Library"
for the Excel 2007 version.

The error I recieve is: "Run-time error '429': ActiveX component can't
create object" and occurs at the Documents.Open() command. I'm guessing it
chokes on opening the word document.

Anyone have any suggestions for correcting this?
 
U

urkec

engineer_rich said:
I am trying to upgrade from Office 2003 to Office 2007. One problem I am
having is opening a Word document from Excel VBA code. It works fine in 2003
version but not in 2007 version.
Here's the code:
-----
Sub OpenMyDocument()
Dim myDoc As String
myDoc = [full path name of my document in quotes]

Dim myDocument As Document

Set myDocument = Documents.Open(myDoc)
MsgBox "opened"
myDocument.Close
MsgBox "closed"

Set myDocument = Nothing
End Sub
-----

I added a reference to "Microsoft Word 11.0 Object Library" for the Excel
2003 version which automatically becomes "Microsoft Word 12.0 Object Library"
for the Excel 2007 version.

The error I recieve is: "Run-time error '429': ActiveX component can't
create object" and occurs at the Documents.Open() command. I'm guessing it
chokes on opening the word document.

Anyone have any suggestions for correcting this?

Try creating Word.Application object first, then use appWord.Documents.Open:

Sub OpenMyDocument()

Dim myDoc As String
myDoc = "c:\scripts\test.doc"

Dim appWord As New Word.Application
Dim myDocument As Word.Document

Set myDocument = appWord.Documents.Open(myDoc)
MsgBox "opened"
myDocument.Close
MsgBox "closed"

Set myDocument = Nothing

End Sub
 
E

engineer_rich

Thank you!

That worked perfectly.

I did add a:
Set appWord = Nothing
at the end to clean up a little.

Thanks again!!

urkec said:
engineer_rich said:
I am trying to upgrade from Office 2003 to Office 2007. One problem I am
having is opening a Word document from Excel VBA code. It works fine in 2003
version but not in 2007 version.
Here's the code:
-----
Sub OpenMyDocument()
Dim myDoc As String
myDoc = [full path name of my document in quotes]

Dim myDocument As Document

Set myDocument = Documents.Open(myDoc)
MsgBox "opened"
myDocument.Close
MsgBox "closed"

Set myDocument = Nothing
End Sub
-----

I added a reference to "Microsoft Word 11.0 Object Library" for the Excel
2003 version which automatically becomes "Microsoft Word 12.0 Object Library"
for the Excel 2007 version.

The error I recieve is: "Run-time error '429': ActiveX component can't
create object" and occurs at the Documents.Open() command. I'm guessing it
chokes on opening the word document.

Anyone have any suggestions for correcting this?

Try creating Word.Application object first, then use appWord.Documents.Open:

Sub OpenMyDocument()

Dim myDoc As String
myDoc = "c:\scripts\test.doc"

Dim appWord As New Word.Application
Dim myDocument As Word.Document

Set myDocument = appWord.Documents.Open(myDoc)
MsgBox "opened"
myDocument.Close
MsgBox "closed"

Set myDocument = Nothing

End Sub
 
U

urkec

engineer_rich said:
Thank you!

That worked perfectly.

I did add a:
Set appWord = Nothing
at the end to clean up a little.

Sorry, I also forgot to quit Word.Application:

....

appWord.Quit
Set appWord = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top