Excel Macro to Print All Open Documents

Last reviewed: August 20, 1995
Article ID: Q67571

The information in this article applies to:

  • Microsoft Excel for Windows, version 2.x, 3.x, 4.x, 5.0
  • Microsoft Excel for Windows 95, version 7.0

SUMMARY

The following Visual Basic procedure (version 5.0 only) and Microsoft Excel macro will batch print all the documents that are currently open in Microsoft Excel:

Visual Basic Procedure

The following procedure prints all of the sheets in each open workbook in Microsoft Excel version 5.0.

Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose. Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide."

   Sub Print_All()
      Dim n As Integer
      Dim c As Integer
      ' Find number of open workbooks
      n = Application.Workbooks.Count
      'Loop through and print each open workbook
      For c = 1 To n
         Application.Workbooks(c).PrintOut
      Next c
   End Sub

Microsoft Excel Macro

(Note that if you run this macro in Microsoft Excel version 5.0, only the active sheet in each open workbook will print)

  1. The macro sheet name enclosed in quotation marks in cell A5 must be the name of the macro sheet on which you put this macro. This is so the macro will not print itself.

  2. The Windows version 3.0 print spooler imposes a 20-file limitation. This macro does not check for circumstances exceeding this maximum. Normally, this should not be a problem because it is close to the maximum number of documents Microsoft Excel can have open (Read/Write under Windows 3.0). Chances are that the spooler will have completed printing some of the files by the time Microsoft Excel sends all the documents to the spooler. The limit in Windows version 3.1 is 100 print jobs.

  3. If the 20-file limitation does pose a problem in practice, the WAIT function may be used to allow the spooler some additional time to send a file to the printer before Microsoft Excel sends another document to the spooler. The following line may be inserted between cells A5 and A6:

          =WAIT(NOW()+TIMEVALUE("00:02:00"))
    

    This line will cause the macro to pause for 2 minutes before sending the next document.

NOTE: Microsoft provides macro examples for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This macro is provided 'as is' and Microsoft does not guarantee that the following code can be used in all situations. Microsoft does not support modifications of the code to suit customer requirements.

The following is the macro:

A1: Print_Docs A2: =COLUMNS(DOCUMENTS()) A3: =FOR("Count",1,A2,1) A4: =ACTIVATE(INDEX(DOCUMENTS(),1,Count)) A5: =IF(GET.DOCUMENT(1)<>"macro1.xlm",PRINT()) A6: =NEXT() A7: =RETURN()


KBCategory: kbusage
KBSubcategory:

Additional reference words: 7.00 2.00 2.01 2.10 3.00 4.00 5.00


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 20, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.