The Document Print Event

The procedure for the document Print event is called before a document is printed. Word, Excel, or PowerPoint will call the document Print event procedure if it's set up in your program before the document is printed. In Word and Excel, this event can be cancelled. Therefore, the name of the event is prefixed with the word Before. In PowerPoint, this event can't be cancelled and thus, the event name isn't prefixed with Before.

You can print documents using the Print command on the File menu or on the Standard toolbar. The PrintOut method on the Document, Workbook, and Presentation objects, described in the previous chapter, also triggers the document Print event procedure. In PowerPoint, the print event also triggers when the PrintOut method on the PrintOptions object, accessed from the Presentation object, is executed.

Sub App_DocumentBeforePrint( _
    ByVal Doc As Document, Cancel As Boolean)
Sub App_WorkbookBeforePrint( _
    ByVal Wb As Workbook, Cancel As Boolean)
Sub App_PresentationPrint(ByVal Pres As Presentation)

Example Scenario

The following examples for Word, Excel, and PowerPoint are similar to the examples described after the Quick Guide section in the document New event procedures. When you print a document, the application adds the text "MyCompany Confidential" to the header at the top left of each page. Because PowerPoint doesn't support headers on a slide, it adds text to the footer of each slide. The added text is formatted to be bolded and its font name set to Arial. You can replace the text string "MyCompany" with the name of your company.

Set Up the DocumentBeforePrint Event in Word

To insert a header in Word, use the Headers property to access the document's header text range. The text "MyCompany Confidential" is inserted at the top left of each page. To view the header, click Print Layout on the View menu and scroll to the top of the page.

  1. In Word, start the Visual Basic Editor and then double-click the Class1 project item in the Project Explorer to make it the active window.
  2. Click App from the Object drop-down list and then select Document-BeforePrint from the Procedures drop-down list in the class module. In the DocumentBeforePrint event procedure, add the following code:
  3. Sub App_DocumentBeforePrint( _
        ByVal Doc As Document, Cancel As Boolean)
    
        With Doc.Sections(1).Headers(wdHeaderFooterPrimary).Range
            .Text = "MyCompany Confidential"
            With .Font
                .Bold = True
                .Name = "Arial"
            End With
        End With
    End Sub
    

  4. In the Project Explorer, double-click the Module1 project item to make it the active window, place the cursor in the InitEvents procedure, and press F5.
  5. Switch to Word and click New on the Standard toolbar to create a new document.
  6. Add some text to the document and click the Print option on the File menu.

Set Up the WorkbookBeforePrint Event in Excel

In Excel, to insert a header or footer you use the PageSetup property on the Worksheet object to access the PageSetup object. On the PageSetup object, you use the LeftHeader property to set the header. To insert text in any header or footer position, you need to apply header and footer information to all the worksheets in a workbook.

  1. In Excel, start the Visual Basic Editor and then double-click the Class1 project item in the Project Explorer to make it the active window.
  2. Click App from the Object drop-down list and then select Workbook-BeforePrint from the Procedures drop-down list in the class module. In the WorkbookBeforePrint event procedure, add the following code:
  3. Private Sub App_WorkbookBeforePrint( _
        ByVal Wb As Workbook, Cancel As Boolean)
    
        Dim sh As Worksheet
        For Each sh In Wb.Worksheets
            sh.PageSetup.LeftHeader = _
                "&""Arial,Bold""MyCompany Confidential"
        Next sh
    End Sub
    

  4. In the Project Explorer, double-click the Module1 project item to make it the active window, place the cursor in the InitEvents procedure, and then press F5.
  5. Switch to Excel and click New on the Standard toolbar to create a new workbook.
  6. Add some text to the workbook and click the Print button on the File menu.

As described in the New event procedure for Excel earlier in this chapter, when you view a worksheet normally, you don't see the header and footer. You only see header and footer information when the worksheet is in Print Preview mode or when it's printed. To see or set headers and footers in Excel, click Page Setup on the File menu and click the Header/Footer tab. In this example, a custom header is set. Therefore, to see the custom header, click the Custom Header button between the Header and Footer previews.

Set Up the PresentationPrint Event in PowerPoint

In this procedure, PowerPoint adds the text "MyCompany Confidential" to the Footer placeholder, which is located at the bottom center of the presentation's slide master by default. The index position of the Footer placeholder in the Placeholders collection of the presentation's slide master is 4 by default. You use the SlideMaster object, accessed through the SlideMaster property on the Presentation object, to set footer information on all the slides in a presentation. All slides, with the exception of title slides, share the same slide master. If you set the text in a placeholder on the slide master, it applies to all slides.

  1. In PowerPoint, start the Visual Basic Editor and then double-click the Class1 project item in the Project Explorer to make it the active window.
  2. Click App from the Object drop-down list and then select PresentationPrint from the Procedures drop-down list in the class module. In the PresentationPrint event procedure, add the following code:
  3. Private Sub App_PresentationPrint(ByVal Pres As Presentation)
        With Pres
            With .SlideMaster.HeadersFooters.Footer
                .Text = "MyCompany Confidential"
                .Visible = msoTrue
            End With
            With .SlideMaster.Shapes.Placeholders(4) _
                    .TextFrame.TextRange.Font
                .Bold = msoTrue
                .Name = "Arial"
            End With
        End With
    End Sub
    

  4. In the Project Explorer, double-click the Module1 project item to make it the active window, place the cursor in the InitEvents procedure, and then press F5.
  5. Switch to PowerPoint and click New on the Standard toolbar to create a new presentation.
  6. Add some text to the presentation and click the Print button on the File menu.

In the preceding PresentationPrint event procedure, the Footer placeholder is accessed on the Shapes collection of the SlideMaster for the presentation. To change the text of this placeholder or any other placeholder on a slide master, select Header and Footer on the View menu and display the Slide tab.

When the Placeholder Doesn't Exist

The index position of the Footer placeholder in the Placeholders collection of the presentation's slide master is 4 by default. For example, the Print event procedure you added in the code example above uses the value of 4. However, if the presentation template applied to the presentation doesn't contain the Footer placeholder on the slide master, or if the user deleted the Footer placeholder on the slide master, you use the custom function PlaceholderShape to determine if a specific placeholder type exists in a presentation and, if it doesn't, to put the placeholder back in the presentation.

To make the document Print event procedure in PowerPoint more robust, you revise the Print event procedure with four lines added below the With Pres statement. The first line declares the variable shpPlaceholder, which is set to the return value of the custom PlaceholderShape function. This function returns the Shape object representing the Footer placeholder. You clear the Footer placeholder's text by setting the TextRange property to an empty string (""). Note also the change in the With statement in the last With…End block in the event procedure. This With…End block sets the font attributes in the placeholder. You use the shpPlaceholder variable in place of .SlideMaster.Shapes _.Placeholders(4).

Retrieve or Add a Placeholder Shape

  1. Replace the code in the PresentationPrint event procedure added in the previous code example with the following PresentationPrint event procedure. Also add the custom function PlaceholderShape after the event procedure.
  2. Private Sub App_PresentationPrint(ByVal Pres As Presentation)
        With Pres
            Dim shpPlaceholder As Shape
            Set shpPlaceholder = PlaceholderShape( _
                .SlideMaster.Shapes, ppPlaceholderFooter, True)
            shpPlaceholder.TextFrame.TextRange = ""
    
            With .SlideMaster.HeadersFooters.Footer
                .Text = "MyCompany Confidential"
                .Visible = msoTrue
            End With
            With shpPlaceholder.TextFrame.TextRange.Font
                .Bold = msoTrue
                .Name = "Arial"
            End With
        End With
    End Sub
    
    Function PlaceholderShape(shpColl As Shapes, _
        iType As PpPlaceholderType, _
        bAddPlaceholderIfMissing As Boolean) As Shape
    
       Dim shpPlaceHolder As Shape
        For Each shpPlaceHolder In shpColl.Placeholders
            If shpPlaceHolder.PlaceholderFormat.Type = iType Then
                Set PlaceholderShape = shpPlaceHolder
                Exit Function
            End If
        Next shpPlaceHolder
        If bAddPlaceholderIfMissing Then
            Set PlaceholderShape = _
                shpColl.AddPlaceholder(Type:=iType)
        End If
    End Function
    

  3. In the Project Explorer, double-click the Module1 project item to make it the active window, place the cursor in the InitEvents procedure, and then press F5.
  4. Switch to PowerPoint and display the slide master by clicking Master on the View menu and then Slide Master from the submenu. In the slide master, delete the Footer placeholder.
  5. Click the Print button on the File menu.

Because the PlaceholderShape function is generic, you can use it to determine if a specific type of placeholder exists on a slide and, if it doesn't, to add the placeholder to the slide or slide master. You need to pass three arguments to the function. The first is the Shapes collection of the slide or slide master on which you want to search for a specific type of placeholder. The second argument is the placeholder type you want to search for. And the third argument is a Boolean indicating whether the placeholder shape should be added if it doesn't exist in the Shapes collection.

In this example, the Footer placeholder will be replaced once the PresentationPrint event procedure is executed. The next chapter provides a full description of placeholders in PowerPoint.