Custom Printing with Scripts and Word

The existing print styles in Outlook 2007 and the capability to define custom styles accommodate the needs of most users. In some situations, however, these built-in printing features are not enough. With a little custom scripting and Word 2007, however, you can overcome these limitations.

Word 2007 offers almost unlimited print layout capabilities, making it a great tool for laying out almost any type of document. For example, assume that you’re not satisfied with the way Outlook 2007 prints messages from mail folders. When you print a message, the recipient’s name appears in large, bold type at the top of the page, as shown in Figure 27-20. You are likely printing your own messages, so you might want different information—such as the message subject—displayed on the first line.

The standard message layout.

Figure 27-20. The standard message layout.

Although you can make minimal changes to the print layout such as adding headers or footers, you can’t do much in Outlook 2007 to change the way most items are printed. You can, however, copy Outlook 2007 items to Word 2007, format the document as needed, and then print it (from Word 2007). You might think that moving the data from Outlook 2007 to Word 2007 is a time-consuming task, but you can make it happen in less than a second—provided you create a script to accomplish the task for you.

Using a Custom Contact Style

In this first example, assume that you want to create a specific layout for contacts that prints the contact name in bold, places the contact’s picture in the upper-right corner, and then includes selected contact information such as address, phone, e-mail, and other properties underneath. Figure 27-21 illustrates this custom print layout.

This contact style presents selected information in a specific layout.

Figure 27-21. This contact style presents selected information in a specific layout.

The first step in creating this custom contact style is to create the document layout in Word 2007. In a nutshell, this means creating a document template that contains a text form field for each contact item that you want included on the printout and then following these steps:

Note

The companion CD contains two sample templates named CustomContactPrint.dotx and CustomMessagePrint.dotx that you can customize to suit your needs or simply use as samples for this example. The code for the macros used in this section is also included (CustomContact.txt and CustomMessagePrint.txt). Create a folder named myMail in the root of the C drive, and then copy these files into that folder so that the macros can locate them.

  1. In Word 2007, open a new document. Click the Microsoft Office Button, and then click Word Options. On the Popular tab, select the Show Developer Tab On The Ribbon check box, and then click OK.

  2. Enter labels for the Outlook 2007 fields. Then position the insertion point after the first label, and on the Developer tab, in the Controls group, select the Rich Text content control to insert a field to contain the data that will come from the Outlook 2007 contact.

  3. Select the first text form field, press the Spacebar to delete the default text, and on the Insert tab, in the Links group, choose Bookmark. In the Bookmark dialog box, shown in Figure 27-22, type a name for the bookmark that identifies the field (such as LastName), and then click Add.

    Add a bookmark for each text form field.

    Figure 27-22. Add a bookmark for each text form field.

  4. Select the remaining fields and add bookmarks for them, naming the bookmarks according to the information the field will contain, such as FirstName, Email, Phone, and so on.

  5. Select the Title field, format it using a larger font, and then add any other characteristics such as bold type.

  6. Draw a line separating the name and other information, if desired.

  7. Click the Microsoft Office Button, click Save As, Word Template, specify the path C:myMail and the name CustomContact.dotx, click Save, and then exit Word 2007.

You now have a template that is ready to be filled in by Outlook 2007. The next step is to create a macro in Outlook 2007 that copies the desired information from the current contact item to the form and then prints the form. The following sample macro accomplishes these tasks. However, this sample does not provide extensive error checking, so consider this a starting point for your own macro. For example, you might want to add code that verifies that the current item is a contact item, and if not, displays an error message and exits.

Note

The following macro is included on the companion CD in the file CustomContactPrint.txt. Before using this macro, you need to create a folder named myMail in the root of the C drive. Copy the Word 2007 templates that are included on the companion CD into that folder so that the macros can locate them.

Choose Tools, Macro, Macros, type CustomContactPrint in the Macro Name field, and then click Create. Enter the following code:

Sub CustomContactPrint()
    'Set up objects
    Dim strTemplate As String
    Dim objWord As Object
    Dim objDocs As Object
    Dim objApp As Application
    Dim objItem As Object
    Dim objAttach As Object
    Dim numAttach As Integer
    Dim objNS As NameSpace
    Dim mybklist As Object
    Dim x As Integer
    Dim pictureSaved As Boolean
    Dim myShape As Object
    'Dim ContactAddress
    'Create a Word document and current contact item object
    Set objApp = CreateObject("Outlook.Application")
    Set objNS = objApp.GetNamespace("MAPI")
    'Check to ensure Outlook item is selected
    If TypeName(objApp.ActiveInspector) = "Nothing" Then
        MsgBox "Contact not open. Exiting", vbOKOnly + vbInformation, "Outlook Inside
 Out"
        Exit Sub
    End If
    Set objItem = objApp.ActiveInspector.CurrentItem
    Set objWord = CreateObject("Word.Application")
    strTemplate = "c:myMailCustomContact.dotx
    Set objDocs = objWord.Documents
    objDocs.Add strTemplate
    Set mybklist = objWord.activeDocument.Bookmarks
    'Fill in the form
    objWord.activeDocument.Bookmarks("LastName").Select
    objWord.Selection.TypeText CStr(objItem.LastName)
    objWord.activeDocument.Bookmarks("FirstName").Select
    objWord.Selection.TypeText CStr(objItem.FirstName)
    If objItem.HasPicture = True Then
        Set objAttach = objItem.Attachments
        numAttach = objAttach.Count
        For x = 1 To numAttach
            If objAttach.Item(x).DisplayName = "ContactPicture.jpg" Then
                objAttach.Item(x).SaveAsFile "C:myMail" & _
                objAttach.Item(x).DisplayName
                pictureSaved = True
            End If
        Next x
        If pictureSaved = True Then
            objWord.activeDocument.Bookmarks("Picture").Select
            Set myShape = objWord.activeDocument.Shapes.AddPicture("c:myMail
ContactPicture.jpg", False, True, 432, -25)
            objWord.activeDocument.Shapes(1).Left = 432 - objWord.activeDocument.
Shapes(1).Width
        End If
    End If
    objWord.activeDocument.Bookmarks("Address1").Select
    objWord.Selection.TypeText CStr(objItem.BusinessAddressStreet)
    objWord.activeDocument.Bookmarks("Address2").Select
    objWord.Selection.TypeText CStr(objItem.BusinessAddressCity)
    objWord.Selection.TypeText ", "
    objWord.Selection.TypeText CStr(objItem.BusinessAddressState)
    objWord.Selection.TypeText " "
    objWord.Selection.TypeText CStr(objItem.BusinessAddressPostalCode)
    objWord.activeDocument.Bookmarks("Spouse").Select
    objWord.Selection.TypeText CStr(objItem.Spouse)
    objWord.activeDocument.Bookmarks("Phone1").Select
    objWord.Selection.TypeText CStr(objItem.BusinessTelephoneNumber)
    objWord.activeDocument.Bookmarks("WebPage").Select
    objWord.Selection.TypeText CStr(objItem.BusinessHomePage)
    objWord.activeDocument.Bookmarks("Email1").Select
    objWord.Selection.TypeText CStr(objItem.Email1Address)
    'Print and exit
    objWord.PrintOut Background:=True

    'Process other system events until printing is finished
    While objWord.BackgroundPrintingStatus
        DoEvents
    Wend
    objWord.Quit SaveChanges:=wdvbaDoNotSaveChanges
    Set objApp = Nothing
    Set objNS = Nothing
    Set objItem = Nothing
    Set objWord = Nothing
    Set objDocs = Nothing
    Set mybklist = Nothing
End Sub

If you examine this macro code, you’ll see that it uses named bookmarks to locate the position in the document where each contact element will be inserted. Also notice that the lines in the macro that insert specific contact items reference those items by their Outlook 2007 object model names, such as Email1Address, BusinessTelephoneNumber, BusinessHomePage, and so on. If you want to modify this macro to insert other contact items, you’ll need to know the item names. To view contact item properties, choose Tools, Macro, Visual Basic Editor, and then choose Help, Microsoft Visual Basic Help. Select Outlook Object Model Reference, and then click ContactItem Object. Click the Properties link, and then scroll through the list of properties to locate the one you need.

This macro also determines whether the contact has a picture associated with it. If so, the macro cycles through the attachments to locate the picture (which is always named ContactPicture.jpg) and saves it to disk. The macro then inserts the picture in the Word 2007 document. Although ContactPicture.jpg is always the first attachment, regardless of the order in which items are attached or their names, this macro checks each attachment anyway to accommodate future changes in Outlook 2007 regarding picture attachments.

Note

The macro saves the picture file in the folder you created for the Word 2007 templates, C:myMail. You can change the macro code to save and load the picture from any path. You can also change the path location for the Word 2007 document template as needed.

To run the macro and print a contact in this format, in the Contacts folder, open the contact you want to print. On the Developer tab, in the Code group, select Macros. (If you have not added the Developer tab to the Ribbon, press Alt+F8.) Select the CustomContactPrint macro, and then click Run. The macro will run and print the open contact on the default printer.

Using a Custom Message Style

You can use a method similar to the one in the preceding section to print messages from Outlook 2007 using Word 2007. For example, assume that you want to print e-mail messages with the subject in large, bold type at the top of the page and with other message content printed below that. Figure 27-23 shows a sample form in Word 2007. Use the same general steps detailed in the preceding section to create a Word 2007 template that contains the form text fields and bookmarks needed to hold the message items.

A sample form in Word 2007 for printing messages.

Figure 27-23. A sample form in Word 2007 for printing messages.

After you create the document template in Word 2007 and save it, open the Microsoft Visual Basic® Editor and create the following macro:

Note

This sample macro is contained on the companion CD as CustomMessagePrint.txt.

Sub CustomMessagePrint()
    'Set up objects
    Dim strTemplate As String
    Dim objWord As Object
    Dim objDocs As Object
    Dim objApp As Application
    Dim objItem As Object
    Dim objNS As NameSpace
    Dim mybklist As Object
    'Create a Word document and current message item object
    Set objApp = CreateObject("Outlook.Application")
    Set objNS = objApp.GetNamespace("MAPI")
    'Check to ensure Outlook item is selected
    If TypeName(objApp.ActiveInspector) = "Nothing" Then
        MsgBox "Message not open. Exiting", vbOKOnly + vbInformation, "Outlook Inside
Out"
        Exit Sub
    End If
    Set objItem = objApp.ActiveInspector.CurrentItem
    Set objWord = CreateObject("Word.Application")
    strTemplate = "c:myMailprnmsg.dotx"
    Set objDocs = objWord.Documents
    objDocs.Add strTemplate
    Set mybklist = objWord.activeDocument.Bookmarks
    'Fill in the form
    objWord.activeDocument.Bookmarks("Title").Select
    objWord.Selection.TypeText CStr(objItem.Subject)
    objWord.activeDocument.Bookmarks("From").Select
    objWord.Selection.TypeText CStr(objItem.SenderName)
    objWord.activeDocument.Bookmarks("Sent").Select
    objWord.Selection.TypeText CStr(objItem.SentOn)
    objWord.activeDocument.Bookmarks("Received").Select
    objWord.Selection.TypeText CStr(objItem.ReceivedTime)
    objWord.activeDocument.Bookmarks("To").Select
    objWord.Selection.TypeText CStr(objItem.To)
    objWord.activeDocument.Bookmarks("Cc").Select
    objWord.Selection.TypeText CStr(objItem.CC)
    objWord.activeDocument.Bookmarks("Subject").Select
    objWord.Selection.TypeText CStr(objItem.Subject)
    objWord.activeDocument.Bookmarks("Body").Select
    objWord.Selection.TypeText CStr(objItem.Body)
    'Print and exit

    objWord.PrintOut Background:=True
    'Process other system events until printing is finished
    While objWord.BackgroundPrintingStatus
        DoEvents
    Wend
    objWord.Quit SaveChanges:=wdvbaDoNotSaveChanges
End Sub

As with the CustomContactPrint macro, you can customize this macro and document template to accommodate different or additional message fields as needed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset