' create macro and connect with button action in PowerPoint
' taken from Example03.vb

Private Sub Example03Main()

        Dim powerApplication As PowerPoint.Application = Nothing
        Dim documentFile As String = Nothing
        Try

' start powerpoint powerApplication = New PowerPoint.Application() ' add a new presentation with one new slide Dim presentation As PowerPoint.Presentation = _
powerApplication.Presentations.Add(MsoTriState.msoTrue) Dim slide As PowerPoint.Slide = presentation.Slides.Add(1, _
PpSlideLayout.ppLayoutBlank) ' add new module and insert macro ' the option "Trust access to Visual Basic Project" must be set Dim vbeModule As VBE.CodeModule = presentation.VBProject.VBComponents.Add( _
vbext_ComponentType.vbext_ct_StdModule).CodeModule Dim macro As String = String.Format("Sub NetOfficeTestMacro()" & vbNewLine & _
" {0}" & vbNewLine & "End Sub", "MsgBox ""Thanks for click!""") vbeModule.InsertLines(1, macro) ' add button and connect with macro Dim button As PowerPoint.Shape = slide.Shapes.AddShape( _
MsoAutoShapeType.msoShapeActionButtonForwardorNext, 100, 100, 200, 200) button.ActionSettings(PpMouseActivation.ppMouseClick).AnimateAction = _
MsoTriState.msoTrue button.ActionSettings(PpMouseActivation.ppMouseClick).Action = _
PpActionType.ppActionRunMacro button.ActionSettings(PpMouseActivation.ppMouseClick).Run = _
"NetOfficeTestMacro" ' save the document Dim fileExtension As String = GetDefaultExtension(powerApplication) documentFile = String.Format("{0}\\Example02{1}", _
Application.StartupPath, _
fileExtension) presentation.SaveAs(documentFile, _
PpSaveAsFileType.ppSaveAsDefault, _
MsoTriState.msoTrue) Catch throwedException As Exception ' not trusted Dim message As String = _
String.Format("An error is occured.{0}ExceptionTrace:{0}", _
Environment.NewLine) Dim exception As Exception = throwedException While (Not IsNothing(exception)) message += String.Format("{0}{1}", _
exception.Message, Environment.NewLine) exception = exception.InnerException End While MessageBox.Show(message) Finally ' close excel and dispose reference powerApplication.Quit() powerApplication.Dispose() If (Not IsNothing(documentFile)) Then Dim fDialog As FinishDialog = _
New FinishDialog("Presentation saved.", documentFile) fDialog.ShowDialog(Me) End If End Try End Sub ''' <summary> ''' returns the valid file extension for the instance. for example ".ppt" or ".pptx" ''' </summary> ''' <param name="application">the instance</param> ''' <returns>the extension</returns> ''' <remarks></remarks> Private Function GetDefaultExtension(ByVal application As PowerPoint.Application) As String Dim version As Double = _
Convert.ToDouble(application.Version, CultureInfo.InvariantCulture)
If (version >= 12.0) Then Return ".pptx" Else Return ".ppt" End If End Function

Last edited Jun 13, 2012 at 2:03 PM by SebastianDotNet, version 8

Comments

No comments yet.