Printing Externally

  • Thread starter Thread starter tess457
  • Start date Start date
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??
 
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.)
 

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

Back
Top