How to print even and odd pages by using VBA in Publisher (294748)



The information in this article applies to:

  • Microsoft Publisher 2002
  • Microsoft Office Publisher 2003

This article was previously published under Q294748

SUMMARY

It is often preferrable to print a publication using both sides of a single sheet of paper. This is called duplex printing. Some printers have a setting to turn the paper automatically and print on both sides. On printers that do not support duplex printing, you can manually perform duplex printing.

Manual duplex printing requires that you print all the odd numbered pages first. When they are finished printing, you have to remove the printed pages from the printer, turn or rotate the pages based on how the printer feeds the paper, and then place the paper back in the printer so that you can print the reverse side.

Microsoft Publisher does not have the ability to print only the even or odd numbered pages in a publication. However, starting with Microsoft Publisher 2002, you can use a Microsoft Visual Basic for Applications (VBA) macro to do manual duplex printing.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
The following sample macro will work with any document that is using either the Full Page or Custom Publication type layout, as long as you are printing one page per printed page. The PrintOut method does not support the use of optional printing parameters when it is called.

PrintOut Method

The PrintOut method prints all or part of the specified document.

Syntax for the PrintOut Method

Use the following syntax for the PrintOut method:
expression.PrintOut(From, To, PrintToFile, Copies, Collate)
				
where expression is an expression that returns one of the objects in the "Applies To" list. Expression is required for the AutoFilter method. The following table lists the optional arguments for the PrintOut method and the descriptions for the arguments.
Argument      Description
-------------------------------------------------------------------------
From          The starting page number. Type: Long.

To            The ending page number. Type: Long.

PrintToFile   The path and file name of a document to be printed to a
              file. Type: String.

Copies        The number of copies to be printed. Type: Long.

Collate       When printing multiple copies of a document, True to print
              all pages of the document before printing the next copy.
              Type: Boolean.
				

Sample Macro

The following sample macro prints the active publication.
Sub PrintActivePublication()
    ThisDocument.PrintOut
End Sub
				
  1. On the Tools menu, point to Macro, and then click Visual Basic Editor.
  2. On the Insert menu, click Module.

    Publisher inserts a module called Module1 into your project.
  3. In the Module window, type the following code:
    Function OddPrint(lCopies As Long) As Boolean
       Dim i As Long
       Dim j As Long
       Dim lPages As Long
       '
       ' Find out how many pages are in the publication.
       '
       lPages = ActiveDocument.Pages.Count
       '
       ' This is the outer loop to repeat the print sequence
       ' for the number of copies set by the user.
       '
       For j = 1 To lCopies
       '
       ' Inner loop, print out each page skipping the even
       ' pages by starting at page one, then incrementing
       ' the page number by two.
       '
          For i = 1 To lPages Step 2
             ActiveDocument.PrintOut i, i
          Next i
       Next j
       '
       ' Return a value of true for the operation.
       '
       OddPrint = True
    End Function
    
    Function EvenPrint(lCopies As Long) As Boolean
       Dim i As Long
       Dim j As Long
       Dim lPages As Long
       '
       ' Find out how many pages are in the publication.
       '
       lPages = ActiveDocument.Pages.Count
       '
       ' This is the outer loop to repeat the print sequence
       ' for the number of copies set by the user.
       '
       For j = 1 To lCopies
       '
       ' Inner loop, print out each page skipping the odd
       ' pages by starting at page two, then incrementing
       ' the page number by two.
       '
          For i = 2 To lPages Step 2
             ActiveDocument.PrintOut i, i
          Next i
       Next j
       '
       ' Return a value of true for the operation.
       '
       EvenPrint = True
    End Function
    
    Sub PrintOddEven()
       Dim lCopies As Long
       '
       ' Prompt to enter the number of copies to be printed.
       '
       lCopies = InputBox("How many copies?", Default:=1)
       '
       ' Print out the odd pages of the publication. If
       ' OddPrint returns True, then continue.
       '
       If OddPrint(lCopies) = True Then
       '
       ' If OK is select to continue printing even
       ' pages, then call the EvenPrint function and pass
       ' it the number of copies to print.
       '
          If MsgBox("Print Even Pages?", vbOKOnly, _
                "Printing Even Pages") = vbOK Then
             EvenPrint (lCopies)
          End If
       End If
    End Sub
    
    					
  4. Quit the Visual Basic Editor.
  5. On the Tools menu, point to Macro, and then click Macros.
  6. In the Macro Name list, click PrintOddEven.
  7. Click Run.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbBilling_AccountQuestions kbAutomation KbVBA kbhowto KB294748