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) |
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.
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.
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 |
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.
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 |
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.
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.
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 |
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.
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).
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 |
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.