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.
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.
Dim m_sldNew As PowerPoint.Slide Dim m_paraItem As Paragraph |
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).
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.
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.
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.
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.
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.
Dim txtTitle As TextRange |
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.
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.
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.
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:
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.
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.
Set oOutlook = CreateObject("Outlook.Application") |
You've now created an Outlook Application object and you're ready to work with it.
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.
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.
.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."
.Display |
The Display method of the MailItem object displays the mail item you created.
.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.
.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.
SendMail |
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: