I am trying to create a custom Powerpoint toolbar that generates a toolbar of useful layouts that I am saving in a 'templates.pptx' deck as I come across them.
The templates.pptx has a series of sections in it, and I am adding the slides into the respective sections. I want the VBA to:
Process menu calls getProcessContent
on load:
Sub getProcessContent()
Dim xml As String
xml = generateXML(1)
MsgBox (xml)
getProcessContent = xml
End Sub
This calls a generic generate XML function that takes a section ID parameter
Function generateXML(sectionId)
Dim template As presentation
Dim path As String
Dim xml As String
Dim sectionName As String
Dim Slide As Integer
Dim slideObj As Slide
Dim slideTitle As String
'Load template layouts
'path = AddIns("km_ppt_toolbar").path
path = "[HIDDEN]\ppt toolbar\v2"
Set template = Presentations.Open(path & "\templates.pptx")
'Generate blank XML string
xml = ""
FirstSlide = template.SectionProperties.FirstSlide(sectionId)
Lastslide = (FirstSlide + template.SectionProperties.SlidesCount(sectionId) - 1)
For Slide = FirstSlide To Lastslide
' Add a button for each slide
Set slideObj = template.Slides(Slide)
slideTitle = slideObj.Shapes.Title.TextFrame.TextRange.Text
slideXML = "<button id=""" & slide & """ label=""" & slideTitle & """ imageMso=""HappyFace"" size=""normal"" onAction=""InsertSlide " & slide & """ />"
xml = xml & slideXML
Next Slide
presentation.Close
generateXML = xml
End Function
When I run the getProcessContent
sub, I get a Run Time Error on the first slide in the For loop, highlighting the slideObj = presentation.Slides(Slide)
line saying "Object doesn't support this property or method"
So @FunThomas helped point me in the right direction on this one, essentially my use of set
or not was, shall we say, wacky...
Fixing the function in question, then threw up another challenge, which I fixed having read a few more bits on the DynamicMenu
element.
Final working set of functions are below. These now generate the XML correctly. Unfortunately the buttons are not displaying in my menu currently. This is an issue, I'm sure, with my XML text and not the function's themselves. Once I figure it out, I will post an update.
' Have to add in IRibbonControl and returnedVal bits in order to pass XML back to ribbon
Sub getProcessContent(control As IRibbonControl, ByRef returnedVal)
Dim xml As String
xml = generateXML(1)
returnedVal = xml
End Sub
Function generateXML(sectionId)
Dim template As presentation
Dim path As String
Dim xml As String
Dim sectionName As String
Dim Slide As Integer
Dim slideObj As Slide
Dim slideTitle As String
'Load template layouts
'path = AddIns("km_ppt_toolbar").path
path = "[HIDDEN]\ppt toolbar\v2"
Set template = Presentations.Open(path & "\templates.pptx")
'Generate blank XML string [EDIT - added menu schema]
xml = "<menu xmlns=""http://schemas.microsoft.com/office/2009/07/customui"">"
FirstSlide = template.SectionProperties.FirstSlide(sectionId)
Lastslide = (FirstSlide + template.SectionProperties.SlidesCount(sectionId) - 1)
For Slide = FirstSlide To Lastslide
' Add a button for each slide
Set slideObj = template.Slides(Slide)
slideTitle = slideObj.Shapes.Title.TextFrame.TextRange.Text
slideXML = "<button id=""" & Slide & _
""" label=""" & slideTitle & _
""" onAction=""InsertSlide " & Slide & """ />"
xml = xml & slideXML
Next Slide
' Add menu close tag
xml = xml & "</menu>"
template.Close
generateXML = xml
End Function