VBA macro to print and close word document will not pause between actions resulting in failure to close application
Posted on 2006-04-02
I have a VBA macro triggered from C# following a mail merge in Word 2000 that should automatically print and close the document:
' Print_Envelope Macro
' Macro created 4/1/2006'
Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
Collate:=True, Background:=True, PrintToFile:=False, PrintZoomColumn:=0, _
PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
Dim PauseTime, Start
PauseTime = 10 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Application.Documents.Close False, False, False
Dim wordApp As Object
Set wordApp = GetObject(, "Word.Application")
Set wordApp = Nothing
The problem I am up against is that each of these 3 macro sets works fine when launched manually one at a time. But if I try to run them together, the print will wait until after the pause, and the close error will still occur. I need the print to occur, then a brief pause of 10 seconds, then close the document. Is there a way to force the print command in the first portion of the macro, then pause for 10 seconds, then perform the last part of the code?