0
votes

im currently having problems with some visual basic stuff. I have successfully added the shape to my current Powerpoint slide using the following code:

dim pres as Microsoft.Office.Interop.Powerpoint
dim slide as pres.ActivePresentation.Slides(1)
With slide.Shapes.AddShape(Type:=Microsoft.Office.Core.MsoAutoShapeType.msoShapeActionButtonCustom, Left:=50, Top:= 50, Width:=70, Height:=30)
    .Name="Test"
End With

So what i want to do now is the function, whenever it is clicked (in presentation mode) it should call a Sub named "test()" defined in the same class. I am not able to that. All i can do for now is call a macro from the presentation using the following code:

dim pres as Microsoft.Office.Interop.Powerpoint
dim slide as pres.ActivePresentation.Slides(1)
With slide.Shapes.AddShape(Type:=Microsoft.Office.Core.MsoAutoShapeType.msoShapeActionButtonCustom, Left:=50, Top:= 50, Width:=70, Height:=30)
    .Name="Test"
    With .ActionSettings(PowerPoint.PpMouseActivation.ppMouseClick)
        .Run = "test"
        .Action = PowerPoint.PpActionType.ppActionRunMacro
    End With
End With

But since i do not have any macros in my presentation this will result in an error. When i manually add a macro using the integrated visual basic editor in powerpoint it just works the way i intended.

Is there any way to directly call a Sub from within my visual basic code without having to create a macro?

I hope i made myself clear and you guys understood my problem.

Thanks in advance.

2

2 Answers

2
votes

i just stumbled upon the solution i was looking for. I did not find a way to refer an action to a powerpoint shape but instead could manage to call a function from an OLEObject. OLEObject have handlers and therefore one can simply use the click-event.

First of all you'll need to add a reference to Forms and your Visual basic for Application, simply do this by adding

Imports System.Windows.Forms
Imports Microsoft.Vbe.Interop

to your code.

Secondly you need to add an OLEObject CommandButton to your slide and add a handler to it, you can achieve this by:

Dim oshape As Microsoft.Office.Interop.PowerPoint.Shape = ppt.ActivePresentation.Slides(1).Shapes.AddOLEObject(Left:=100, Top:=100, _
        Width:=150, Height:=50, ClassName:="Forms.CommandButton.1")
    With oshape.OLEFormat.Object
        .Name = "Button1"
        .Caption = "Test"
        .Font.Bold = True
        .Font.Name = "Verdana"
        .Object.BackColor = RGB(25, 25, 50)
    End With
    AddHandler CType(oshape.OLEFormat.Object, MSForms.CommandButton).Click, _
          AddressOf Button1_Click

I found a very helpful answer at vbarchiv.

Thanks for your help

0
votes

Is your code running from within a macro-enabled presentation or an add-in?

My understanding of what you want to do is this:

  1. Programmatically add a shape to a slide, setting its action methods to run the macro "test" then clicked in slide show mode.
  2. Programmatically add the macro "test" to the presentation.

If this is correct, step 2 requires the following:

  1. That the presentation (.pptx) is resaved as a macro-enabled file e.g. .pptm, .potm, ppsm
  2. That PowerPoint is set up to "Trust access to the VBA project object model" under File / Options / Trust Center / Trust Center Settings / Developer Macro Settings
  3. That you add a reference to the Microsoft Visual Basic for Applications Extensibility 5.3 library
  4. That you then use the VBA object model (which requires the reference above) to the to create a code module and then insert the procedure in the VBA project. Start exploring the object model here : ActivePresentation.VBProject

This adds a standard code module:

ActivePresentation.VBProject.VBComponents.Add vbext_ct_StdModule

This adds a procedure from a string (you can do it from a file too):

ActivePresentation.VBProject.VBComponents(1).CodeModule.AddFromString