Reputation: 29
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.
Upvotes: 0
Views: 3572
Reputation: 29
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
Upvotes: 2
Reputation: 2999
Is your code running from within a macro-enabled presentation or an add-in?
My understanding of what you want to do is this:
If this is correct, step 2 requires the following:
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
Upvotes: 0