Creating a PowerPoint Presentation from a Word Document

After creating a report using Word, you may find that its breakdown of topics could easily be integrated into a PowerPoint presentation. Word enables you to send your active Word document to PowerPoint simply by pointing to Send To on the File menu and then clicking Microsoft PowerPoint on the submenu. Unfortunately, the result may not match your Word template's formatting. However, you can use Visual Basic for Applications to create your own Send To PowerPoint feature. This way, you can customize the output to meet your needs.

NOTE
The practice files for Chapter 7 already include a Word document that has the code needed to generate both a PowerPoint presentation and an Outlook e-mail message that the presentation is attached to. You can load the practice file, browse through the code in the Visual Basic project of the Word document, and run the code to see the results.

Create Source Information and Define Module-Level Variables

  1. Switch to Word.
  2. When you install Word, the Normal template that's attached to new blank documents contains a short list of predefined text styles: Heading 1 through Heading 9, Normal, and a few others. To see this list of text styles, click Style on the Format menu, and then select All Styles from the List drop-down list. In the Styles box, you should see a full list of the Word text styles defined in the Normal template. You can also see an abbreviated list by clicking the Style drop-down list on the Format toolbar.

  3. In the default document (Document1 or a similar name), add a few one-line paragraphs and format each line as one of these text styles: Heading 1, Heading 2, Heading 3, and Heading 4. Use the following illustration as a guide. You can also copy these lines from the CreatePP.doc file.
  4. Click to view at full size.

  5. In the Visual Basic Editor of Word, insert a new code module.
  6. On the Tools menu, click References, select the Microsoft PowerPoint 9.0 Object Library from the Available References list, and click OK.
  7. In the declarations section (the top) of the code module, add the following module-level variable declarations:
  8. Dim m_sldNew As PowerPoint.Slide
    Dim m_paraItem As Paragraph
    

  9. Add the following module-level constant declaration:
  10. Const m_sPresFile As String = _
    "C:\Temp\MyPres.ppt"
    

    The module-level constant m_sPresFile is set equal to the filename given to the slide presentation that you will create. Save the presentation to your hard disk so that you'll be able to use the file as an attachment in an Outlook e-mail message (as you'll do later in this chapter).

Write the Code to Examine the Headings

  1. Create a new procedure in the code module by typing Sub Main and pressing ENTER, and then add the following declarations and a Set statement within the procedure:
  2. Dim appPPT As New PowerPoint.Application
    Dim pres As PowerPoint.Presentation
    Dim sStyle As String
    
    Set pres = appPPT.Presentations.Add(WithWindow:=msoFalse)
    

    Note the use of the keyword New in the first object variable declaration. The first time you use the variable appPPT in code, the program creates a PowerPoint Application object and implicitly sets it to the object variable appPPT.

    The last line represents the first time that appPPT is referenced in code. Thus, the PowerPoint Application object is automatically created. You use the Application object to access the Presentations collection object, which consists of the list of presentations currently open in PowerPoint, whether or not they're visible to the user. The Add method of the Presentations object creates a new presentation.

  3. Add the following With…End block containing the For Each…Next loop:
  4. With ActiveDocument.Range
        For Each m_paraItem In .Paragraphs
        Next m_paraItem
    End With
    

    You'll use this loop to iterate through each paragraph in the active Word document.

  5. Just after the line For Each m_paraItem In .Paragraphs, add the following code as the first line in the For Each…Next loop:
  6. sStyle = m_paraItem.Style
    

    The Style property of the Paragraph object in Word returns a string that represents the name of the text style used in the paragraph. The string is assigned to the variable sStyle.

  7. Add the following Select Case block:
  8. Select Case sStyle
    Case "Heading 1"
        Set m_sldNew = pres.Slides _
            .Add(pres.Slides.Count + 1, ppLayoutText)
        SetText ppTitleStyle, 1
    Case "Heading 2"
        SetText ppBodyStyle, 1
    Case "Heading 3"
        SetText ppBodyStyle, 2
    Case "Heading 4"
        SetText ppBodyStyle, 3
    End Select
    

The Select Case block examines the value of the string variable sStyle. If the value of sStyle is Heading 1, it creates a new slide in the PowerPoint presentation. The Add method of the Slides collection object accepts two arguments. The first argument is the index position of the newly added slide in the presentation, and the second is the slide layout type. In this case, you always create a slide with Title and Body placeholders and add it to the end of the Slides collection. The text of a paragraph in Word with the text style Heading 1 is used as the slide title in the first newly created slide.

For each case in the above Select Case block, you call the SetText procedure and pass it two arguments: an integer (an enumeration value in the form of a built-in PowerPoint constant) representing the text style and an integer representing the paragraph indent level in the body-text placeholder of a PowerPoint slide. Heading 1 represents a slide title; Heading 2 represents a first-level paragraph in the body-text placeholder; Heading 3 represents a second-level paragraph in the body-text placeholder; and Heading 4 represents a third-level paragraph in the body-text placeholder.

Generate a Correctly Formatted PowerPoint Slide from Word Text

  1. Below the Main procedure, create a new procedure by typing Sub SetText (iTextStyle As Integer, iIndentLevel As Integer) and pressing ENTER.
  2. This procedure places the text from the Word document in the appropriate slide placeholder in the PowerPoint presentation. The code between the parentheses declares the arguments to the procedure. Note that the way you declare arguments is similar to the way you declare variables.

  3. Add the following declaration within the procedure:
  4. Dim txtTitle As TextRange
    

  5. Create the first half of an If…Then…Else statement by adding the following lines of code:
  6. If iTextStyle = ppTitleStyle Then
        Set txtTitle = m_sldNew.Shapes.Title _
            .TextFrame.TextRange
        With txtTitle
            .Text = m_paraItem.Range
            .Text = .TrimText
        End With
    

    The If…Then statement determines the value of the Integer variable iTextStyle, which was passed as an argument to the SetText procedure from the Main procedure. The program uses the text style in the Word document to determine the value of iTextStyle. If the text style of the paragraph in the Word document is Heading 1, it sets the equivalent text style to the title style of a PowerPoint slide.

    The program sets the object variable txtTitle to the text range of the title shape on the PowerPoint slide. The With…End block following the Set statement sets the actual text in the title shape to the text in the paragraph in the Word document. The TrimText method of the TextRange object in PowerPoint removes any spaces, carriage returns, or linefeeds from the text.

  7. Complete the If…Then…Else statement by adding the following lines of code:
  8. Else
        m_sldNew.Shapes.Placeholders(2).TextFrame _
            .TextRange.InsertAfter(m_paraItem.Range) _
            .IndentLevel = iIndentLevel
    End If
    

    If the text style of the paragraph in the Word document isn't Heading 1, the program adds the paragraph's text to the body shape in the PowerPoint slide. The second placeholder for a slide with the Bulleted List layout (specified by the constant ppLayoutText in Visual Basic code) represents the body shape. The program inserts the paragraph from the Word document into the body shape's text range after the body shape's last paragraph.

    The IndentLevel property of the TextRange object in PowerPoint determines how many times to indent the bulleted point in the shape. The program passes the value of the Integer variable iIndentLevel as an argument to the SetText procedure from the Main procedure, and the value is determined by the text style in the Word document.

  9. Finally, add the following With…End block to the end of the Main procedure:
  10. With appPPT
        If .Visible = msoTrue Then
            pres.NewWindow
        Else
            .Visible = msoTrue
        End If
        pres.SaveAs m_sPresFile
        .Activate
    End With
    

    The If…Then…Else condition block sets the PowerPoint application window to Visible if it's not currently displayed. If the application window is currently visible, the new presentation is displayed using the NewWindow method. The line after the If...Then...Else condition block saves the presentation to the location specified by the module-level string constant m_sPresFile. This constant was previously set to "C:\Temp\MyPres.ppt." You may have specified a different file location in the declaration of the constant. The final line activates the PowerPoint application window and puts it in front of all the windows on the screen.

  11. Place the cursor in the Main procedure and press F5 to run it.
  12. After iterating through the Word document and determining the text style of each paragraph in the document, your program uses the text to create a new PowerPoint presentation that should look like the following:

    Click to view at full size.

  13. Exit PowerPoint. (Changes have already been saved.)

Send Your Presentation in E-Mail

Once you've automatically created your PowerPoint presentation, you might want to send it automatically to a specific audience. Using the Outlook object library, you can easily extend your program to create an Outlook e-mail message that contains the presentation as an attachment.

NOTE
In the following example, the e-mail message you create with Visual Basic for Applications uses Outlook and doesn't work for any other e-mail client. You also need to be connected to a network or the Internet to actually send e-mail. As long as you have Outlook installed on your computer, however, you can still run the code in this example without sending the e-mail message you create.

  1. In the same Visual Basic for Applications project, in the Visual Basic Editor of Word, click References on the Tools menu, select the Microsoft Outlook 9.0 Object Library from the Available References list, and click OK.
  2. Below the SetText procedure, create a new procedure by typing Sub SendMail and pressing ENTER.
  3. In the SendMail procedure, add the following two declarations:
  4. Dim oOutlook As Outlook.Application
    Dim oMessage As Outlook.MailItem
    

    You could use the New keyword in the declaration of the object variable oOutlook just as you did when you created the PowerPoint Application object. When you don't use the New keyword, you have to explicitly set the oOutlook object variable using the CreateObject function.

  5. Just below the added declarations, add the following Set statement with the CreateObject function:
  6. Set oOutlook = CreateObject("Outlook.Application")
    

    You've now created an Outlook Application object and you're ready to work with it.

  7. Just below the Set statement with the CreateObject function, add the following:
  8. Set oMessage = oOutlook.CreateItem(olMailItem)
    

    You declared the object variable oMessage as an Outlook MailItem object and, using the Set statement, assigned the oMessage object to a newly created mail item that you're ready to work with. The CreateItem method is a member of the Outlook Application object. CreateItem accepts one of seven possible values, each represented by an enumeration value (in the form of a built-in Outlook constant), which is a predefined name for an Integer or Long value.

  9. Add the following With…End block:
  10. With oMessage
        .To = "Executive Committee"
        .Subject = "New Sales Report"
        .Body = "The following presentation reflects" & _
            " the final sales figures for Q2." & vbCrLf
    End With
    

    The With…End block allows you to set properties of the mail item without having to continuously prefix each property of the MailItem object with oMessage. This both improves performance (because Visual Basic needs to determine what oMessage is just once) and improves your code's readability. The first line in the With…End block sets the To property of the MailItem object to equal the e-mail alias "Executive Committee." (Of course, you should set this to a valid e-mail alias on your e-mail system.) The subject of the e-mail message is set using the Subject property, and the body text of the mail item is set using the Body property. The constant vbCrLf is a Visual Basic constant representing a "carriage return and linefeed" in the text you specify.

  11. At the end of the With…End block, add the following line just before the line End With:
  12. .Attachments.Add m_sPresFile, , , "Q2 Sales"
    

    The MailItem object provides an Attachments collection to which you can add items or iterate through the attachments already contained in the mail item. In this case, you'll add the PowerPoint presentation you previously created. The third argument in the Add method of the Attachments object, called the Position argument, indicates to the Add method after which character position within the body text it should add the attachment. By default, if nothing is specified (as in the above line), the Add method adds the attachment after the last character in the MailItem.Body text. The fourth argument, DisplayName, represents the text used with the icon of the attachment. By default, the program uses the filename of the attachment. Here, you explicitly set it to "Q2 Sales."

  13. Just after the method to add attachments, add the following line before End With:
  14. .Display
    

    The Display method of the MailItem object displays the mail item you created.

  15. Now add the following line as the last line in the With…End block:
  16. .Send
    

    The Send method of the MailItem object sends the newly created e-mail message automatically. However, instead of sending the item now, you'd probably prefer to store it in your Outlook Drafts folder until you're ready to send it manually.

  17. If you want to store the e-mail item in Outlook, replace .Send with the following line:
  18. .Close (olSave)
    

    The Close method then closes the mail item, so you no longer see it on the screen. The olSave constant specified in the argument of the Close method saves the mail item to the item type's Outlook default folder. In this case, it's saved to the Drafts folder in your Outlook mailbox.

    Now that you've created your e-mail procedure, you need to call the procedure that creates and sends the mail item from the same code that automatically generates the PowerPoint presentation.

  19. In the Main procedure, add the following line just before End Sub:
  20. SendMail
    

  21. Place the cursor in the Main procedure and press F5.
  22. In the Main procedure, once the code finishes creating the PowerPoint presentation based on your Word document, it saves the presentation file to your hard disk and then calls the SendMail procedure. The SendMail procedure creates a new mail item in Outlook and adds the generated presentation as an attachment, as shown here:

    Click to view at full size.

  23. Exit all Office applications. Save the changes to Word if you like.