#1
|
|||
|
|||
Pass Arguments to Function Through Ribbon
Hey all,
I'm wondering how to pass arguments to a function through a ribbon button. I have an array of buttons in a self-created ribbon group. I have each button assigned to open a different document in a series. So the first button would obviously open the first document, the second button would open the second, etc. down the list. The functions to open these docs are identical aside from the file names. So why have 18 functions and growing? Why not save space and programming time just maintaining one, to which I can pass an integer argument, and the same function can manage opening whatever document is requested based on what button is clicked? But how do I pass the function an argument through Word's ribbon? I'm running Windows 8 with Microsoft Word 2010. I've done searches on Google and taken books out from the library to learn this, and I've come up with nothing. This leads me to believe either I don't understand what I'm reading when the answer is in front of me (I'm not a book learner) or this kind of thing is impossible. Thanks, everyone. Last edited by inagalaxyfarfarawry; 03-14-2015 at 04:43 PM. |
#2
|
|||
|
|||
Hey everyone,
I want to know if there is a way to pass an argument to a VB function in Microsoft Word 2010. I have a self-made ribbon tab created, and in it is a group with an array of buttons that each opens a document in a series. The first button would obviously open the first document in the series, the second button would open the second, etc. all the way down to 18 (and growing as time goes on). As it stands, I have 18 different macros all doing the exact same thing except opening a different document in the series. It would save space (and maintenance time) to have one macro that could open up any one of the documents depending on what button was pressed. But to do that, I need to pass it an argument, a document_number integer (or whatever). Is there a way to do this in MS Word 2010? I've tried to Google an answer and checked books at the library, but I'm coming up short. I'm not terribly book smart, so the answer could have been staring me in the face, but I misread or misapplied it, or this could be impossible. Last edited by inagalaxyfarfarawry; 03-14-2015 at 04:43 PM. |
#3
|
|||
|
|||
What is your question? How much effort do you expect people to devote to your issue based on the effort that you made to define it?
|
#4
|
|||
|
|||
Not exactly sure how you're missing the question. My problem is defined above...twice, I might add, since it looked like this site hadn't taken it the first time. How much more do you want?
Quote:
|
#5
|
|||
|
|||
There was nothing above my post when I replied earlier. Just the thread title.
I'm assuming you have a common button OnAction macro defined in your RibbonX and a Button OnAction macro something like the following in your VBA project: Code:
Sub ButtonOnAction(control As IRibbonControl) Dim pCall As String Select Case control.ID Case "Btn1", "Btn2", "Btn3", "Btn4" '... etc. modMain.DoSomethingWithThisButton control.Tag 'Use the control tag property to define a variable argument. Case Else 'Do Nothing End Select End Sub |
#6
|
|||
|
|||
Not sure why there was nothing there. I even accidentally posted it twice in the same thread.
This is the first I've heard about iRibbonControl. I have to confess to being a VB rookie, and programming is not my forte to boot. I've always found programming waaaaay more confusing than other people have. I put your code into my VB, but I'm not sure how to call ButtonOnAction because I don't know what argument to put in the calling sub. I get the error message "ByRef argument type mismatch." I know I'm putting an object type as the argument instead of an instance, but I don't know how to reference iRibbonControl; if it's supposed to be something VB is passing, shouldn't it already have a name? I have to admit I've Googled Object.ID, System.ID, Application (which returns, simply, "Microsoft Word"), Office.ribbonUI, ByVals, examples, everything, and I'm more lost than when I started. Shouldn't this be as easy as assigning test(1) to a ribbon button instead of just test and having to go through all these programming gymnastics just to do something so simple? sub test() call ButtonOnAction(iRibbonControl) end sub |
#7
|
|||
|
|||
You don't call ButtonOnAction. It is the VBA callback for your RibbonX control. Here is a basic example:
This is RibbonX to build a basic tab with two buttons: Code:
customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"> <ribbon> <tabs> <tab id="custTab1"insertBeforeMso="TabHome"label="Demo Tab"> <group id="Grp0"label="Demo Group"> <button id="Grp0Btn1"tag="1"label="Demo Button 1" imageMso="SlideThemesGallery"size="large" onAction="modRibbonControl.ButtonOnAction" /> <button id="Grp0Btn2"tag="1"label="Demo Button 1" imageMso="SlideThemesGallery"size="large" onAction="modRibbonControl.ButtonOnAction" /> </group> </tab> </tabs> </ribbon> </customUI> Code:
Sub ButtonOnAction(control As IRibbonControl) Dim arrParts() As String Select Case control.ID Case "Grp0Btn1", "Grp0Btn2" 'Here are two ways using the defined ID or the defined tag: '1 arrParts = Split(control.ID, "Btn") modMain.SomeSubThatTakesArgurments arrParts(1) '2 modMain.SomeSubThatTakesArgurments control.Tag End Select lbl_Exit: Exit Sub End Sub Code:
Sub SomeSubThatTakesArgurments(ByRef strArg As String) Select Case strArg Case "1": MsgBox "Do this" Case "2": MsgBox "Do that" End Select lbl_Exit: Exit Sub End Sub See: http://gregmaxey.com/word_tip_pages/...bbon_main.html |
#8
|
|||
|
|||
Here's an example of what's in the XML of one of my buttons. It doesn't look too similar or different from what you have. Does this make sense?
Quote:
|
#9
|
|||
|
|||
It looks like you are trying to do this with the built-in ribbon editor. I told you already that won't work.
|
#10
|
|||
|
|||
Custom Ribbon and Callbacks
Here is what I have as an example for a dropdown list. The ribbon xml (use the Custom UI Editor mentioned in Ron de Bruin's website. I am doing this in MS-Word
Ribbon XML: Code:
<!-- This is ribbon customization --> <customUI onLoad="rbn_onLoad" xmlns="http://schemas.microsoft.com/office/2009/07/customui"> <ribbon> <tabs> <!-- Add Custom tab to the ribbon --> <tab id="tabDropdownExample" label="My Custom Tab" insertAfterMso="TabDeveloper"> <group id="grpDropdown" label="Rib Controls"> <dropDown id="ddDropdown" sizeString="String length is desired control width" onAction="RbnOnAction_Dropdown" label="Dev Dropdown" getSelectedItemIndex="RbnGetSelectedItemIndex_Dropdown" getEnabled="RbnGetEnabled_Dropdown" getVisible="RbnGetVisible_Dropdown"> <item id="DropdownDefault" label="My Dropdown:"/> <item id="dd1" label="Item 1"/> <item id="dd2" label="Item 2"/> </dropDown> <button id="TestButton1" getLabel="RbnGetLabel_TestButton1" size="normal" onAction="RbnOnAction_TestButton1" imageMso="InspectTasksMenu" screentip="Used for testing"/> </group> </tab> <!-- End of Add Custom tab to the ribbon --> </tabs> </ribbon> </customUI> Code:
Option Explicit Public Const DblSp As String = vbCrLf & vbCrLf 'Double line space for MsgBox Public rib As IRibbonUI Private Const RibPtrName As String = "MyRibbonPtr" '**** Begin functions to restore the ribbon pointer value 'Credit to Ron de Bruin https://www.rondebruin.nl/index.htm 'with minor changes to store the pointer value in a document variable. #If VBA7 Then Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long) #Else Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long) #End If #If VBA7 Then Function GetRibbon(ByVal lRibbonPointer As LongPtr) As Object #Else Function GetRibbon(ByVal lRibbonPointer As Long) As Object #End If Dim objRibbon As Object CopyMemory objRibbon, lRibbonPointer, LenB(lRibbonPointer) Set GetRibbon = objRibbon Set objRibbon = Nothing End Function ''Word calls this when it loads the file because the RibbonX '' specified it: onLoad="rbn_onLoad" Public Sub rbn_onLoad(ribbon As IRibbonUI) 'Capture the ribbon variable for later use, specifically to invalidate it. 'When you invalidate the ribbon Word recreates it. Set rib = ribbon On Error Resume Next ThisDocument.Variables(RibPtrName).Delete 'Note: variables are typed as strings in Word. ThisDocument.Variables.Add RibPtrName, Value:=ObjPtr(rib) On Error GoTo 0 'rib.ActivateTab "tabDropdownExample" End Sub Private Sub ShowPointer() Dim ptrVal As LongLong On Error Resume Next ptrVal = CLngLng(ThisDocument.Variables(RibPtrName).Value) MsgBox "Rib pointer = " & ptrVal End Sub 'Should be called by an event handler(?) or in a routine to make VBA recreate the ribbon 'Can also be run manually. Public Sub RedoRib() Dim ptrVal As LongLong On Error Resume Next ptrVal = CLngLng(ThisDocument.Variables(RibPtrName)) On Error GoTo 0 If ptrVal = 0 Then MsgBox "The ribbon pointer is lost. Ribbon customizations can not be updated." & DblSp _ & "Unless the ribbon commands you are looking for are missing, " _ & "everything will still work ok." & DblSp _ & "Otherwise, save your work, close the file and reopen it to restore " _ & "ribbon customizations." Exit Sub End If If rib Is Nothing Then Set rib = GetRibbon(ptrVal) rib.Invalidate MsgBox "The Ribbon handle was lost." & DblSp _ & "Hopefully this is restored now by the GetRibbon function." Else rib.Invalidate End If 'rib.ActivateTab "tabDropdownExample" 'This works End Sub '**** End of functions to restore the ribbon pointer value '***** Begin callbacks for custom tab id="tabDropdownExample" '*** Begin callbacks for id="DropDown" Public Sub RbnOnAction_Dropdown( _ ByRef ctrl As Office.IRibbonControl, _ ByRef dropdownID As String, _ ByRef selectedIndex As Variant) Dim msg As String 'Note: selectedIndex is zero-based 'Clicking the default item on control will not trigger this routine. Select Case selectedIndex Case 0 msg = "Item 0 selected." Case 1 msg = "Item 1 selected." Case 2 msg = "Item 2 selected." Case Else msg = "Something weird happened." End Select MsgBox msg RedoRib End Sub Public Sub RbnGetSelectedItemIndex_Dropdown( _ ByRef ctrl As IRibbonControl, _ ByRef returnedVal As Variant) 'Note: selectedIndex is zero-based 'Clicking the default item on control will not trigger this routine. returnedVal = 0 'Desired default item to be selected End Sub Public Sub RbnGetEnabled_Dropdown( _ ByRef ctrl As Office.IRibbonControl, _ ByRef Enabled As Variant) Const strCase As String = "beEnabled" Select Case strCase Case "beEnabled" Enabled = True Case Else Enabled = False End Select End Sub Public Sub RbnGetVisible_Dropdown( _ ByRef ctrl As Office.IRibbonControl, _ Visible As Variant) Const blVisible As Boolean = True If blVisible Then Visible = True Else Visible = False End If End Sub '*** End of callbacks for DropDown '***** Begin callbacks for TestButton1 Public Sub RbnOnAction_TestButton1(ctrl As IRibbonControl) MsgBox "TestButton1 works." RedoRib End Sub Public Sub RbnGetLabel_TestButton1( _ ByRef ctrl As Office.IRibbonControl, _ ByRef Label As Variant) Label = "Test Button1" End Sub '***** End of callbacks for custom tab id="tabDropdownExample" |
#11
|
|||
|
|||
Look at Greg's page on dynamic ribbon menus.
Dynamic Ribbon Menu by Greg Maxey Conceivably, you could have templates for your different documents stored in a folder and have Word give you a dynamic menu of those templates on one button. Here is such a menu. Everything on it is a template. Change the templates, change the menu. This is not a one-day project and you will need to learn a lot, but long-term it is worth doing. Again, it is not something done through the user interface. The menus above have worked for my office for more than ten years through five different versions of Word.
Start with Greg's basic page: Customize the Office Ribbon (It doesn't take rocket science) by Greg Maxey. Be prepared for some frustration. |
#12
|
|||
|
|||
You could also save your files as building blocks and use a custom building blocks gallery on your Ribbon or QAT.
Build & Deploy Custom Building Block Galleries by Greg Maxey Not everything has to be a vba solution. |
Tags |
macro, microsoft word 2010, ribbon |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to Pass Arguments in a Formula? | tinfanide | Excel Programming | 2 | 10-13-2014 06:10 AM |
How to Pass Document to this Sub | ilcaa72 | Word VBA | 1 | 01-28-2014 03:04 PM |
How to import the customized ribbon to Word without overwriting the existing Ribbon? | SharonSh | Word VBA | 0 | 09-26-2013 11:47 PM |
How to pass parameters to a .msg file? | HereNow | Outlook | 0 | 11-05-2012 10:38 AM |
Excel 2010 Ribbon look like 2007's Ribbon | esotop | Excel | 0 | 03-22-2011 07:05 PM |