Adding Text to the Chart Slides

In this example, we'll use a different slide template for our text and chart. Figure 8-14 shows the empty template slide in PowerPoint.

Click to add title

• Click to add text

• Click icon to add chart u

Figure 8-14. PowerPoint slide template for text and chart

Let's begin coding the CreateChartSlidesText procedure. In it we will add the title text in the title placeholder. We'll put our descriptive text in the text placeholder on the left side of the slide. Finally, we'll place our chart in the chart placeholder on the right side of the slide.

1. Open the VBE by selecting the Developer ribbon > Code tab > Visual Basic command, or press by Alt+F11.

2. On the standard module containing the PowerPoint code you've been working on, create a new subroutine and name it CreateChartSlidesText.

3. Add the following variable declarations:

Dim i As Integer

Dim sTitle As String

Dim oShape As PowerPoint.Shape

Dim top As Integer

Dim left As Integer

Dim height As Integer

Dim width As Integer

The first two variables, i and sTitle, serve the same function that they did in our original example. The first is a counter for our loop through our charts and text indexes, and the second will hold the title for each slide. The remaining variables will be used to hold the information for the third of the three Placeholder objects on our slide template. We'll need them in order to place the chart correctly on the slide.

4. Add the following empty For...Next block with one or two blank lines within the code block:

5. Within the For...Next block, add the following code:

Worksheets(1).ChartObjects(i).Activate sTitle = ActiveChart.ChartTitle.Text

Set m_oPptSlide = m_oPptShow.Slides.Add(i + 1, ppLayoutTextAndChart)

This code is almost identical to our previous example—but notice the new layout type enum, ppLayoutTextAndChart. This gives us the slide template shown in Figure 8-14.

6. Next (still within the For...Next loop), add the following With...End With block to the CreateChartSlidesText subroutine:

With m_oPptSlide.Shapes.Placeholders(1) With .TextFrame.TextRange

.Text = sTitle End With

End With

Again, this code is very similar to our previous example, minus the variable to hold the placeholder location (sngChartStart). In our current example, we already have a placeholder for our chart, but we have to use a different technique to get its location.

7. Immediately below the With...End With block, add the following code:

With m_oPptSlide.Shapes.Placeholders(2) With .TextFrame.TextRange

.Text = GetSubjectBody(i) End With

End With

This code sets a reference to the second Placeholder object on our slide and inserts the descriptive text from the GetSubjectBody function.

8. Add the following line of code after the With...End With block we just added:

Worksheets(1).ChartObjects(i).Copy

This line of code copies the current Chart object onto the Windows clipboard for later pasting into our PowerPoint slide template.

So we've got our text elements in place and our chart sitting in memory waiting to be dropped into our slide template. However, we can't just paste our chart into the third placeholder area on our template as we could with the text-based Placeholder objects. To place the chart, we have to get the coordinates of the third placeholder (top, left, height, and width). Then we remove the Placeholder object and paste in the chart, placing it accordingly.

9. Add the following lines of code after the Copy command you just added:

Set oShape = m_oPptSlide.Shapes(3) With oShape top = .Top left = .Left width = .Width height = .Height .Delete End With

Here, we are setting oShape to hold the third shape, which is the chart placeholder. Then we are storing its dimensions and location in our top, left, width, and height variables. Once we have that information, we are deleting the Shape object using its Delete method.

10. Immediately after this code, add the following code:

With m_oPptSlide.Shapes.Paste .Top = top .Left = left .Width = width End With

This code, which places and sizes the chart, is similar in function to our previous example. We are placing it in the exact location of the placeholder we just removed.

Note You might notice that although we're retrieving and holding a reference to the height of the placeholder shape, we're not using it when we place the chart. It's included here for reference. If you need to resize the height in your projects, this is how and where you'd do it.

That's it for coding our For...Next loop.

11. Place the insertion point at the end of the loop and press Enter.

12. Add the following line of cleanup code:

Set oShape = Nothing

Once we're done with it, we destroy the oShape object. That's the last line of code in this procedure. The finished CreateChartSlidesText subroutine should look like Listing 8-12.

Listing 8-12. Complete CreateChartSlidesText Subroutine

Sub CreateChartSlidesText()

Dim i As Integer

Dim sTitle As String

Dim oShape As PowerPoint.Shape

Dim top As Integer

Dim left As Integer

Dim height As Integer

Dim width As Integer

For i = 1 To 3 Worksheets(1).ChartObjects(i).Activate sTitle = ActiveChart.ChartTitle.Text

Set m_oPptSlide = m_oPptShow.Slides.Add(i + 1, ppLayoutTextAndChart)

With m_oPptSlide.Shapes.Placeholders(1) With .TextFrame.TextRange

.Text = sTitle End With End With

With m_oPptSlide.Shapes.Placeholders(2) With .TextFrame.TextRange

.Text = GetSubjectBody(i) End With End With

Worksheets(1).ChartObjects(i).Copy

Set oShape = m_oPptSlide.Shapes(3) With oShape top = .Top left = .Left width = .Width height = .Height .Delete End With

With m_oPptSlide.Shapes.Paste .Top = top .Left = left .Width = width End With

Next i

Set oShape = Nothing End Sub

All that's left to do now is modify our calling procedure, MakePowerPointPresentation, and then run it.

13. Navigate to the MakePowerPointPresentation subroutine.

14. Replace this line of code: CreateChartSlides with this line: CreateChartSlidesText

15. Place the insertion point anywhere inside the MakePowerPointPresentation procedure.

16. Click the Run button on the VBE toolbar or press F5.

17. Once the message box appears, make PowerPoint the active window if it's not already. Figures 8-15 through 8-17 show the chart slides with the descriptive text.

0 0

Responses

Post a comment