Mit VB.NET PowerPoint Präsentation erstellen

Di, 20.04.2010 - 14:21 -- admin

Mit Visual Studio bzw. der Express Version kann man direkt auf Office Produkte zugreifen und die Anwendung von "außen" fernsteuern. So habe ich habe ich Beispielhaft mal ein Programm geschrieben, welches einen Liste von unterschiedlichen Texten animiert und nach ein anderen auf einer PowerPoint Vorlage platziert. Die erstellte PowerPoint Präsentation wird anschließend automatisch in einer Endlosschleife im Vollbild gestartet.
Gedacht ist die ganze Geschichte übrigens für einen Begrüßungsbildschirm, so muss nicht jeden morgen ein PowerPoint Datei erstellt werden, sondern es kann einfach ein Text eingegeben werden, den Rest erledigt das Programm.

Verweise

Damit das Programm kompiliert werden kann, muss man Verweise auf die entsprechenden COM-Objekte in Visual Studio hinzufügen. Je nach Office Version sind unterschiedliche Verweise nötig. Die eigentliche Struktur der Schnittstelle kann unter den Office Versionen etwas variieren, wie man im Quelltext erkennen kann.

Quelltext

''On Office 2007
''You need reference to:
''Microsoft PowerPoint 12.0 Object Library
''Microsoft Office 12.0 Object Library
'Imports Microsoft.Office.Interop
'Imports Microsoft.Office.Interop.PowerPoint
 
 
''On Office 2003
''You need reference to:
''Microsoft PowerPoint 11.0 Object Library
''Microsoft Office 11.0 Object Library
Imports PowerPoint
 
''' <summary>
''' This Example will open a PowerPoint File (Template) add some text and run the generated presentaion in fullscreen.
''' Each text object will animate and show one the given time
''' </summary>
''' <remarks></remarks>
Module PowerPointShapeExample
    Sub main()
        Dim App As New PowerPoint.Application
 
        'force app to be visibly
        App.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
        App.Activate()
 
        'open ppt template out of the application path
        App.Presentations.Open(System.AppDomain.CurrentDomain.BaseDirectory & "\vorlage.ppt")
 
        'set textarray to show and animate
        Dim TextLines() As String = {"Welcome", "on this", "funny day"}
 
 
        For Each line As String In TextLines
            AddText(App, line)
        Next
 
        'Add blank Text to the end
        'so presentaion doesnt stop and restart on the last text object
        AddText(App, " ")
 
        'start powerpoint presentation in fullscreen
        App.ActivePresentation.SlideShowSettings.Run()
    End Sub
 
    ''' <summary>
    ''' PowerPoint: This function will add a Shape with the given text and animate it
    ''' </summary>
    ''' <param name="PPTApp">A PowerPoint Application Object with a open PPT file</param>
    ''' <param name="text">A Text to add and animate</param>
    ''' <param name="ShowSeconds">How long should the text be visible? in sec</param>
    ''' <remarks></remarks>
    Sub AddText(ByVal PPTApp As PowerPoint.Application, ByVal text As String, Optional ByVal ShowSeconds As Integer = 10)
        With PPTApp.ActiveWindow.Selection
            'set the position on the presentation sheet
            'the position depends on your sheet size, so change it
            .SlideRange.Shapes.AddLabel(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 106.75, 81.25, 14.5, 36).Select()
            .ShapeRange.ScaleWidth(41, Microsoft.Office.Core.MsoTriState.msoFalse)
            .ShapeRange.TextFrame.WordWrap = Microsoft.Office.Core.MsoTriState.msoFalse
            .ShapeRange.TextFrame.TextRange.Text = text
            .ShapeRange.TextFrame.TextRange.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter
 
            With .TextRange.Font
                .Name = "Arial Black"
                .Size = 32
                '.Bold = Office.MsoTriState.msoTrue
                .Shadow = Microsoft.Office.Core.MsoTriState.msoCTrue
            End With
 
            With .ShapeRange.AnimationSettings
                .Animate = Microsoft.Office.Core.MsoTriState.msoTrue
                '.EntryEffect = PpEntryEffect.ppEffectFlyFromBottom
                .EntryEffect = PpEntryEffect.ppEffectZoomIn
                .TextLevelEffect = PpTextLevelEffect.ppAnimateByAllLevels
                .AnimateBackground = Microsoft.Office.Core.MsoTriState.msoCTrue
                .AdvanceMode = PpAdvanceMode.ppAdvanceOnTime
                .AdvanceTime = ShowSeconds
                .AfterEffect = PpAfterEffect.ppAfterEffectHideOnClick
            End With
 
        End With
    End Sub
End Module