Using Excel Macro to Print Word Documents

G

Guest

I am trying to set up a macro to print an Excel document. This was easy
enough but the Excel document has links to word documents that display the
visuals that accompany the Excel text. The macro will need to Print the word
documents with the Excel spread sheet. My current macro stops as soon as
word is opened as if there is no communication link between Excel and Word.
I basically just need the macro (in Excel) to open, print, and close a word
document. Any ideas? Help!
 
S

Steve Yandl

Mike,

With your VBE window open, click 'Tools > References' and put a check by
"Microsoft Word 11.0 Object library" (note, it may not be 11.0 if you have a
different version of Office than me) The example below opens Word if it
wasn't already open, opens and prints the document "C:\Scripts\Test1.doc",
closes the document and if Word wasn't open in advance it closes the Word
application. You may want to change the line that makes the Word
application visible so you don't see the window flash but this version will
give you a sense of the timing.

_____________________________________

Sub PrintWordDoc()
Dim bStarted As Boolean
Dim oApp As Word.Application

On Error Resume Next
Set oApp = GetObject(, "Word.Application")
'Get the running instance of Word, if there is no instance
'create a new one:
If Err <> 0 Then
bStarted = True
Set oApp = CreateObject("Word.Application")
End If

oApp.Activate
oApp.Visible = True

oApp.Documents.Open ("C:\Scripts\Test1.doc")

oApp.PrintOut

oApp.ActiveDocument.Close wdDoNotSaveChanges

'Quit only when Word was not running when we started this code
If bStarted Then
oApp.Quit
End If


Set oApp = Nothing
End Sub

_______________________________________

Steve
 
G

Guest

Thanks a bunch, Steve. That worked. I really appreciate your help. Hope
you had a good weekend,
--
Michael J. Nowak
Energy Systems/Dow Chemical
Texas City, TX


Steve Yandl said:
Mike,

With your VBE window open, click 'Tools > References' and put a check by
"Microsoft Word 11.0 Object library" (note, it may not be 11.0 if you have a
different version of Office than me) The example below opens Word if it
wasn't already open, opens and prints the document "C:\Scripts\Test1.doc",
closes the document and if Word wasn't open in advance it closes the Word
application. You may want to change the line that makes the Word
application visible so you don't see the window flash but this version will
give you a sense of the timing.

_____________________________________

Sub PrintWordDoc()
Dim bStarted As Boolean
Dim oApp As Word.Application

On Error Resume Next
Set oApp = GetObject(, "Word.Application")
'Get the running instance of Word, if there is no instance
'create a new one:
If Err <> 0 Then
bStarted = True
Set oApp = CreateObject("Word.Application")
End If

oApp.Activate
oApp.Visible = True

oApp.Documents.Open ("C:\Scripts\Test1.doc")

oApp.PrintOut

oApp.ActiveDocument.Close wdDoNotSaveChanges

'Quit only when Word was not running when we started this code
If bStarted Then
oApp.Quit
End If


Set oApp = Nothing
End Sub

_______________________________________

Steve
 

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