Follow up question on Printing Word Document from Excel Macro...
Posted by Bill Tanner on December 16, 1999 9:52 AM
I was using the code that Tom Morales suggested below and it worked fine, whether I was printing on document or more than one.
What I am doing is using Excel to print insurance policies. The worksheets include those forms which need to be completed -- they are completed by formula -- and the code is supposed to print those worksheets as well as open Word and print some forms from there. The code also controls the print order -- I print a couple of worksheets, print a couple of documnets, more worksheets, more documents, etc. So I am constantly switching back and forth between printing in Excel and printing in Word.
What is happening is that I print the first worksheets just fine. Then I run a subroutine that prints some documents and that works. Then I print more worksheets. Finally, I run another subroutine to print more documents and that is where it chokes. Specifically, it chokes on the first "Set mydoc" statement of the second subroutine -- no matter which one is placed second. The subroutines run fine individually; it's only when called back to back that they choke. The Error Message is "Automation Error 440" -- '-2147023174 (800706ba)' if that helps.
The first subroutine reads:
Sub Print_Policy_Validation()
Dim AppWord As Word.Application
Set AppWord = CreateObject("Word.Application.8")
AppWord.Visible = True 'This could be false, also
'Print Policy Validation Form
Set mydoc = Documents.Open(FileName:="C:\My Documents\Doc1.doc")
With AppWord
.ActiveDocument.PrintOut Background:=False
.Quit SaveChanges:=False 'Quit & close without changes
End With
Set AppWord = Nothing 'release reference to it
End Sub
and the second reads:
Sub Print_AK_Common() Set mydoc = Documents.Open(FileName:="C:\My Documents\Doc2.doc") .Quit SaveChanges:=False 'Quit & close without changes Set AppWord = Nothing 'release reference to it
Dim AppWord As Word.Application
Set AppWord = CreateObject("Word.Application.8")
AppWord.Visible = True 'This could be false, also
'Print Common Policy Conditions, Alaska Cancellation Endorsement and Alaska Attorney's Fees Endorsement
With AppWord
.ActiveDocument.PrintOut Background:=False
Set mydoc = Documents.Open(FileName:="C:\My Documents\Doc3.doc")
With AppWord
.ActiveDocument.PrintOut Background:=False
Set mydoc = Documents.Open(FileName:="C:\ Word\My Documents\Doc4.doc")
With AppWord
.ActiveDocument.PrintOut Background:=False
End With
End With
End With
End Sub
Anybody have any suggestions?
Thanks very much
Bill Tanner