Microsoft KB Archive/67571

= Macro to Print All Open Documents =

Article ID: 67571

Article Last Modified on 11/16/2006

-

APPLIES TO


 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0 Standard Edition
 * Microsoft Excel 4.0c
 * Microsoft Excel 3.0a
 * Microsoft Excel 2.10d
 * Microsoft Excel 2.1 Standard Edition
 * Microsoft Excel 2.01
 * Microsoft Excel 2.0 Standard Edition

-



This article was previously published under Q67571





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 &quot;Programming Style in This Manual&quot; section in the &quot;Document Conventions&quot; section of the &quot;Visual Basic User's Guide.&quot;

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(&quot;00:02:00&quot;))

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(&quot;Count&quot;,1,A2,1)

A4: =ACTIVATE(INDEX(DOCUMENTS,1,Count))

A5: =IF(GET.DOCUMENT(1)<>&quot;macro1.xlm&quot;,PRINT)

A6: =NEXT

A7: =RETURN

Additional query words: 2.00 2.01 2.10 3.00 4.00 Excel

Keywords: KB67571

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.