Automate PowerPoint Slide Creation
One time I have to generate a lot of graphs on fly and insert them to PowerPoint slides to create a nice presentation. The situation is that I have to repeat this process again and again every week or days. The graphs generated on fly are a fixed set. Nobody want to do this boring work again and again. So I developed a VBA script in PowerPoint. I can use it to automate the process. Here are some pieces that are relevant to the auntomatic process.
First of all, we need insert new slide.
' create a new blank slide ActiveWindow.View.GotoSlide Index:=ActivePresentation.Slides.Add(Index:=1, Layout:=ppLayoutTitle).SlideIndex ActiveWindow.Selection.SlideRange.Layout = ppLayoutBlank
Second, we insert pictures to each slide and put them in fixed position precisely.
ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="picture1.jpg", LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, left:=12, top:=12, Width:=770, Height:=690).Select ' set size of picture With ActiveWindow.Selection.ShapeRange .Fill.Transparency = 0# .Height = 262.38 .Width = 293# .left = 100# .top = 50# End With
Third, you may need insert textboxes to the slide. Here is sample code.
ActiveWindow.Selection.SlideRange.Shapes.AddTextbox(msoTextOrientationVerticalFarEast, 29.875, 84, 36.125, 348).Select ActiveWindow.Selection.ShapeRange.TextFrame.WordWrap = msoTrue With ActiveWindow.Selection.TextRange.ParagraphFormat .LineRuleWithin = msoTrue .SpaceWithin = 1 .LineRuleBefore = msoTrue .SpaceBefore = 0.5 .LineRuleAfter = msoTrue .SpaceAfter = 0 End With ActiveWindow.Selection.ShapeRange.ScaleHeight 0.93, msoFalse, msoScaleFromBottomRight ActiveWindow.Selection.TextRange.ParagraphFormat.Alignment = ppAlignCenter ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters(Start:=1, Length:=0).Select With ActiveWindow.Selection.TextRange .Text = "Sample graphes" With .Font .NameAscii = "Arial" .Size = 18 .Bold = msoFalse .Italic = msoFalse .Underline = msoFalse .Shadow = msoFalse .Emboss = msoFalse .BaselineOffset = 0 .AutoRotateNumbers = msoFalse .Color.SchemeColor = ppForeground End With End With ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Font.Bold = msoTrue ActiveWindow.Selection.Unselect
If you need move to slide to slide, the following code can do that.
ActiveWindow.View.GotoSlide (i)
In the second step, when we position graphs, we need pixel units instead of inches PowerPoint uses. To convert inches in PowerPoint slide to pixels, the following formula can be used:
number in inches / 2.54 * 72
By using this formula, you can precisely position your graphs to slides.
