MS Word

Display the Print Dialog Box with the "Current Page" or "Pages" Option Selected

The Problem:

When I'm working on a document, I often want to finish editing a page, print it, check it, and then print another page. But the Print dialog box always appears with the All option selected in the Page Range group box.

The Solution:

With a short macro (see Example 8-12), you can make the Print dialog box appear with the Current Page option, the Pages option, or the Selection option selected instead. If you select the Pages option, you can specify the range of pages to print, as in the example.

Macro to display the Print dialog box with print options preselected

Example 8-12
 Sub Display_Print_Dialog_Custom()
     With Dialogs(wdDialogFilePrint)
         .Range = wdPrintRangeOfPages
         .Pages = "3,5,7-11"
         If .Show = -1 Then .Execute
     End With
 End Sub

The With statement works with the wdDialogFilePrint member of the Dialogs collectionin other words, the Print dialog box. The commands inside the With statement control the options selected:

  • To select the Current Page option, use this statement:

        .Range = wdPrintCurrentPage
    
  • To select the Pages option and specify a simple range of pages, use these statements, substituting suitable numbers on the From and To lines:

        .Range = wdPrintFromTo
        .From = "2"
        .To = "4"
    
  • To select the Pages option and specify a complex range of pages, use these statements, substituting suitable numbers on the Pages line:

        .Range = wdPrintRangeOfPages
        .Pages = "3,5,7-11"
    

The Show method displays the dialog box and returns a value for the button the user clicks. If the value is -1, which indicates that the user clicked the OK button, the Execute command executes the instructions contained in the dialog box. If the user clicks the Cancel button, VBA does not execute the instructions.

by BrainBellupdated
Advertisement: