VBA macro to print and close word document will not pause between actions resulting in failure to close application

Posted on 2006-04-02
Last Modified: 2008-01-09
I have a VBA macro triggered from C# following a mail merge in Word 2000 that should automatically print and close the document:

Sub PrintEnvelope()
' 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
'Pause Task
    Dim PauseTime, Start
    PauseTime = 10    ' Set duration.
    Start = Timer    ' Set start time.
    Do While Timer < Start + PauseTime
        DoEvents    ' Yield to other processes.
'Close Document
   Application.Documents.Close False, False, False
    Dim wordApp As Object
    Set wordApp = GetObject(, "Word.Application")
    Set wordApp = Nothing
End Sub

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?
Question by:Arrummzen
    LVL 17

    Accepted Solution

    There is a function called onTime where you can set it to call a macro after a specified amount of time has passed eg

    Application.OnTime When:=Now + TimeValue("00:00:15"), _

    This should work for tyour project.  One thing to note is that you can only have ONE onTime call working at once.


    LVL 1

    Author Comment

    Works perfect....  Thanks!

    Featured Post

    A Knowledge Base That Stays Up-to-Date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
    Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
    In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
    This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now