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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Activation of Excel VBA to Print Word Document 1

Status
Not open for further replies.

PBAPaul

Programmer
Aug 3, 2002
140
GB
I have Excel VBA code that opens a Word 2010 document and calls the Word macro. The Word macro is a simple print program which I have checked and it works as it should.

If I step through the Excel macro then everything works as intended. However if I attempt to run the macro, it gets to the "oWord.Run MacroName" line and then I get the following error display!

Excel_Error_hggtbv.jpg


Code:
Sub PrintWordDoc()
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    
    Set oWord = CreateObject("Word.Application")
    Set oDoc = GetObject("D:\_DATA\My_Doc.docm")
    oWord.Visible = True

    oWord.Run MacroName:="PrintCarryDoc"
    oWord.ActiveDocument.Save
    oWord.Quit

    Set oWord = Nothing

End Sub

Can anybody help please? It is driving me demented!
 
Hi,

Having instantiated the oWord application object, it would be better if the next statement were...
Code:
Set oDoc = oWord.Documents.Open("D:\_DATA\My_Doc.docm")
...otherwise the oWord object has no relation to the oDoc object.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Out of curiosity...
When you[tt]
Dim oWord As Word.Application[/tt] (early binding)
your oWord at this point is a Word.Application object.

Then why:[tt]
Set oWord = CreateObject("Word.Application")[/tt]
Again? [ponder]


Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
>your oWord at this point is a Word.Application object

It isn't, you know. At this point no object has been instantiated.
 
If you want to avoid having the line:
Set oWord = CreateObject("Word.Application")
you could use:
Dim oWord As New Word.Application

That way, a new Word instance is created when you first reference it, as in:
Set oDoc = oWord.Documents.Open("D:\_DATA\My_Doc.docm")

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top