Tip 2: Creating an Instance of PowerPoint
Kicking off GenPPT is easy; just add a reference to
PowerPointTools.dll to your project and then run the following code:
// Create instance of GenPPT
PowerPointTools oPPT = new PowerPointTools();
oPPT.LaunchPPT();
The code for
LaunchPPT creates new instances for PowerPoint 2003 and Excel. Note that the following method utilizes the object property references to
oPPTApp and
oExcel, which other methods in the class will also use:
Public Sub LaunchPPT()
Me.oPPTApp = New PowerPoint.Application
Me.oPPTApp.Visible = False
Me.oExcel = New Excel.Application
Me.oExcel.Visible = False
Me.SlideNumber = 0
End Sub
Tip 3: Starting a New Presentation with a Template and a Title Page
After creating an instance of PowerPoint, you can create a new presentation and specify the template and a title page, using three public methods from GenPPT:
oPPT.SetTemplate("C:\\MyTemplates\\star.pot");
oPPT.BuildTitlePage("2003 Kansas City Chiefs", _
"By Kevin S. Goff");
oPPT.AddPicture("C:\\KCchiefs.gif");
First, the GenPPT code for
SetTemplate provides your first exposure to the PowerPoint object model:
Public Sub SetTemplate(ByVal TemplateFilename As String)
Me.oPPTPres = Me.oPPTApp.Presentations.Add
Me.AddSlide(PowerPoint.PpSlideLayout.ppLayoutTitle)
Me.oPPTPres.ApplyTemplate(TemplateFilename)
SetTemplate adds a new presentation and stores an object reference to
oPPTPres. The method also calls one of GenPPT's support methods,
AddSlide:
Public Sub AddSlide(ByVal oLayout As PowerPoint.PpSlideLayout)
' Increment the active slide number,
' add a new slide based on that number/layout,
' and go to the slide
Me.SlideNumber = Me.SlideNumber + 1
oPPTApp.ActivePresentation.Slides.Add( _
Me.SlideNumber, oLayout)
oPPTApp.ActiveWindow.View.GotoSlide(Me.SlideNumber)
End Sub
Second, the GenPPT code for
BuildTitlePage utilizes another internal method called
AddText to place text inside predefined shapes for the current slide layout:
Public Sub BuildTitlePage(ByVal MainTitle As String,
ByVal SubTitleTemplate As String)
Me.AddText("Rectangle 2", MainTitle)
Me.AddText("Rectangle 3", SubTitleTemplate)
End Sub
Finally, GenPPT contains the
AddPicture method, which displays an image on the center of the current slide:
Public Sub AddPicture(ByVal PicFile As String)
Dim oBitmap As Bitmap
oBitmap = New Bitmap(PicFile)
 | |
Figure 1. The opening slide (uses layout style). |
Dim PicWidth As Int32 = oBitmap.Width
Dim PicHeight As Int32 = oBitmap.Height
Dim StartTop As Int32 = (540 - PicHeight) / 2
Dim StartWidth As Int32 = (720 - PicWidth) / 2
Me.oPPTApp.ActiveWindow.Selection.SlideRange.
Shapes.AddPicture(PicFile, False, True, _
StartWidth, StartTop)
End Sub
So far, this leads to the generation of the first title slide, in
Figure 1.
PowerPoint output should not be confused with general business reporting. Each slide should be a synthesized and self-contained unit, and must not reference information on other slides.
|
|
As you may have gathered, GenPPT contains wrapper functions so that a developer doesn't have to access the PowerPoint object model directly. However, you may find a time when you need to access the PowerPoint object reference (Table 1) directly. If so, you'll need to add a COM reference to the PowerPoint Object Model Library. I'll show an example of doing this later in the article.
Tip 4: Creating a Text-Only Slide with a Title and a Set of Bullet Points
The next step is to create a table of contents slide with a title at the top and bullet points in the body of the slide. GenPPT must account for a variable-sized list of bullet points, and that some may be indented.
GenPPT provides a method called
BuildBulletPage for generating individual slides with bullet point content (see
Figure 2). To use this method, create a DataTable with two columns: one for the bullet point text, and an integer column to represent the level of indentation (use
1 for a main bullet point, and increment for each level of indentation when needed).
 | |
Figure 2: The opening slide (generating bullet points). |
DataTable DtBullets = new DataTable();
DtBullets.Columns.Add("Bullets", typeof(String));
DtBullets.Columns.Add("Indent", typeof(Int32));
DtBullets.Rows.Add("Final AFC West Standings",1);
DtBullets.Rows.Add("Offensive Leaders", 1);
DtBullets.Rows.Add("Receptions by Receiver", 2);
DtBullets.Rows.Add("Receptions by Receiver", 2);
DtBullets.Rows.Add("Rushing Yardage Chart", 2);
DtBullets.Rows.Add("Division QB Ratings", 2);
DtBullets.Rows.Add("Margin of Victory Chart ",1);
DtBullets.Rows.Add("Game Photos", 1);
oPPT.BuildBulletPage("Table of Contents", DtBullets);
The
BuildBulletPage method in GenPPT shown below uses the
ppLayoutText slide layout to build a bullet page. The method adds a new slide, sets the slide title, and scans through the DataTable. The method examines the Indent column to set the
IndentLevel for each bullet point:
Public Sub BuildBulletPage(ByVal MainTitle As String, _
ByVal DtBulletPoints As DataTable)
' Note the indentlevel property, from the IndentLevel in the
' datatable source
Me.AddSlide(PowerPoint.PpSlideLayout.ppLayoutText)
Me.AddText("Rectangle 2", MainTitle)
Me.oPPTApp.ActiveWindow.Selection.SlideRange.Shapes( _
"Rectangle 3").Select()
Dim TextCounter As Int32 = 0
For Each Dr As DataRow In DtBulletPoints.Rows
oPPTApp.ActiveWindow.Selection.TextRange.InsertAfter(
Dr(0) + Chr(13))
TextCounter += 1
oPPTApp.ActiveWindow.Selection.TextRange.Paragraphs(
TextCounter, 1).IndentLevel = (Dr(1))
Next
End Sub