Wednesday, April 01, 2009

Powerpoint Macros with Aspose

This macro never worked for me because I didn't want to bother with the add-in that runs stuff when powerpoint starts.  Instead I made a text box in my template with the alt-text "instructions" and told people to hit F5, then click the box to run the macro that refreshes OLE Objects (excel charts) and also tables that for whatever reason were showing in the normal view, but not presentation view.  This shouldn't be needed for Aspose .NET since you can just use Aspose.Charts and render a picture of your actual chart to replace the "Object Changed" image that is the default.  Of course, I don't know if that strange occasional table bug affects the .NET version.

Sub Image1_Click()
    Dim ashape As Shape
    load_all_charts
    For Each ashape In ActivePresentation.Slides(1).Shapes
        If ashape.AlternativeText = "instructions" Then
            ashape.Delete
        End If
    Next
End Sub
Sub load_all_charts()
    Dim oShape As Shape
    Dim oSlide As Slide
    Dim oGraph As Object
    ActivePresentation.NewWindow.Activate
    ' Loop through each slide in the presentation
    For Each oSlide In ActivePresentation.Slides
        ActiveWindow.View.GotoSlide (oSlide.SlideNumber)
        ' Loop through all the shapes on the current slide.
        For Each oShape In oSlide.Shapes
            ' Check whether the shape is an OLE object.
            If oShape.Type = msoEmbeddedOLEObject Then
                ' Found an OLE object; run edit on it
                oShape.Select
                oShape.OLEFormat.DoVerb (0)
            End If
            If oShape.Type = msoTable Then
                oShape.Select
                oShape.Ungroup
                oSlide.Shapes.Range().Regroup
            End If
        Next oShape
    Next oSlide
    ActiveWindow.Close
    ActivePresentation.SlideShowWindow.Activate
End Sub
 
 Just set your instructions' text box onclick method to Image1_Click and make sure your users enable macros (or sign your presentation so they don't have to lower their security settings and macros are enabled automatically).  Also, don't forget that Aspose only preserves macros in the Presentation object you got by loading your template.  No creating a new presentation from scratch and copying slides to it.  Yes, this means you waste time loading the template file repeatedly.

No comments: