Add macro to slide with .Net

  • Thread starter Thread starter kerb
  • Start date Start date
K

kerb

In VB.net, I am trying to add an object (button) from the control toolbox on
a slide, and then I want to add the click and other events to it via VB.net.
When I try to add code to the object it gets put in a new module rather than
in the slide area when you view the VB editor.

For instance, (in vb.net)
-------------------------------
dim msg as string="hello"

dim s as string = _
"Private Sub CommandButton1_Click()" & vbcrlf & _
" ' some code here" & vbcrlf & _
"End Sub" & vbcrlf

oPres.VBProject.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule).CodeModule.AddFromString(s)
----------------------------------

But this does not add the macro to the right area. It adds a new module.
To see what it should look like, if you place a button on a slide and then
click on it, you can see where this same code is placed.

Thanks
 
Hi,
You cannot explicitly add a document module associated with a slide in
PowerPoint. When you add a active control on the slide, the document module
is inserted automaticallty. To add code into it, you will need to retreive
the reference. The VBComponent type to check for is vbext_ct_Document and
check it's name property.


--
Regards,
Shyam Pillai

Animation Carbon: Copy/Paste/Share animation libraries.
www.animationcarbon.com
 
Unless I'm mistaken, you'd want to add the event handlers and other code to the Slide
object that contains the controls. This example builds the code in a string; you may
prefer to add it from a file instead:

Sub AddSomeCode()

Dim x As Long
Dim sCode As String
Dim sSlideName As String

' Build the code; this assumes there's a cmdClickMe object
' on the slide and that it has a click event:
sCode = "Private Sub cmdClickMe_Click()" & vbCrLf
sCode = sCode & "MsgBox " & Chr$(34) & "You clicked?" & Chr$(34) & vbCrLf
sCode = sCode & "End Sub" & vbCrLf

sSlideName = "Slide1" ' name is assigned by PPT

With ActivePresentation.VBProject

For x = 1 To .VBComponents.Count
With .VBComponents(x)
Debug.Print .Name
If .Name = sSlideName Then
.CodeModule.AddFromString (sCode)
End If

End With
Next
End With



'
oPres.VBProject.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdMod
u
' le).CodeModule.AddFromString(s)
End Sub
 
Back
Top