Printing Externally

T

tess457

is there a way to print a word document externally. Say I am in an exce
file, and every time I print my excel file, it has to print a certai
word file. Is there a way of doing this??
 
D

Dave Peterson

Excel has an event that fires when you print the workbook.

Rightclick on the excel icon to the left of File (on the worksheet menubar).
Select view code and paste this in:

Option Explicit
Private Sub Workbook_BeforePrint(Cancel As Boolean)

Dim oWord As Object
Dim myWordDocName As String
Dim testStr As String
Dim WordWasAlreadyRunning As Boolean

myWordDocName = "c:\my documents\word\test.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myWordDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox myWordDocName & " doesn't exist"
Exit Sub
End If

WordWasAlreadyRunning = True
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err Then
Set oWord = CreateObject("Word.Application")
WordWasAlreadyRunning = False
End If

oWord.Visible = True 'nice for testing
oWord.Documents.Open myWordDocName
oWord.activedocument.PrintOut
oWord.activedocument.Close False

If WordWasAlreadyRunning Then
'do nothing
Else
oWord.Quit
End If

Set oWord = Nothing

End Sub

Then try it out. (I'd use a single sheet word document -- just to save some
trees.)
 
Top