PDA

View Full Version : PowerPoint VBA to get value from Ribbon Control



andyk001
11-12-2021, 12:06 PM
Hi,
I am a bit new to PowerPoint VBA and wondering if its possible to Design a ribbon addin with either dropdown list or combo box and then use the values to run some sub routine.
So I have created PowerPoint Ribbon Addin with a DropDown with items showing as "Depart 1", "Depart 2" & "Depart 3" and then one button to say import excel. So Idea is user can first select which department they want to prepare the slides for. Once the department is selected then they can run the report which will copy the charts from the designated excel.
Below code is an example to show what I am trying to do. In this when I select department in drop down list; I get the message which department is selected from Drop Down. But when I want to use that as variable, my code did not recognise it.

Below is XML code I am having for Ribbon Addin:




<ribbon>
<tabs>
<tab id="CustomTab" label="TestLabel">
<group id="SampleGroup" label="BU Selection">
<dropDown id="DdropDown" label="Select Dept" onAction="DReportSelection" getSelectedItemIndex = "DropDown_OnGetSelectedItemIndex">
<item id="item1" label="Depart 1" />
<item id="item2" label="Depart 2" />
<item id="item3" label="Depart 3" />
</dropDown>
</group >
<group id="SampleGroup1" label="TestLabel">
<button id="Button" label="Import Excel" imageMso="MicrosoftExcel" screentip="Import Data from Excel" size="large" onAction="ExcelImport" />
</group >
</tab>
</tabs>
</ribbon>
</customUI>



Below is VBA I am having in my presentation.


Sub BUReportSelection(control As IRibbonControl, ID As String, selectedindex As Variant)
Dim department As String
department = Choose(selectedindex + 1, "Depart 1", "Depart 2", "Depart 3")

MsgBox department & " is selected ", vbInformation
End Sub
----------------------------------------------------------------------------------------
Sub ExcelImport()

If department = "Depart 1" Then
Call Macro4Department 1
End If
If department = "Depart 2" Then
Call Macro4Department 2
End If
If department = "Depart 3" Then
Call Macro4Department 3
End If
End Sub


Please note above code is just to demonstrate what I am trying to do.
Thanks for your Help in advance.
Andy

Paul_Hossler
11-14-2021, 08:05 PM
Maybe you can integrate something like this



Option Explicit

Dim ddIndex As Long
Dim ddDept As String

'Callback for DdropDown onAction
Sub DReportSelection(control As IRibbonControl, id As String, index As Integer)
ddIndex = index
End Sub

'Callback for Button onAction
Sub ExcelImport(control As IRibbonControl)
Macro4Department (ddIndex)
End Sub

Sub Macro4Department(i As Long)
Select Case i
Case 0
MsgBox "Department 1 is selected ", vbInformation
Case 1
MsgBox "Department 2 is selected ", vbInformation
Case 2
MsgBox "Department 3 is selected ", vbInformation
End Select
End Sub




<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"><ribbon>
<tabs>
<tab id="CustomTab" label="TestLabel">
<group id="SampleGroup" label="BU Selection">
<dropDown id="DdropDown" label="Select Dept" onAction="DReportSelection">
<item id="item1" label="Depart 1" />
<item id="item2" label="Depart 2" />
<item id="item3" label="Depart 3" />
</dropDown>
</group >


<group id="SampleGroup1" label="TestLabel">
<button id="Button" label="Import Excel" imageMso="MicrosoftExcel" screentip="Import Data from Excel" size="large" onAction="ExcelImport" />
</group >
</tab>
</tabs>
</ribbon>
</customUI>