Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-14-2015, 08:47 AM
inagalaxyfarfarawry inagalaxyfarfarawry is offline Pass Arguments to Function Through Ribbon Windows 8 Pass Arguments to Function Through Ribbon Office 2013
Novice
Pass Arguments to Function Through Ribbon
 
Join Date: Mar 2015
Posts: 8
inagalaxyfarfarawry is on a distinguished road
Exclamation 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.
Reply With Quote
  #2  
Old 03-14-2015, 10:54 AM
inagalaxyfarfarawry inagalaxyfarfarawry is offline Pass Arguments to Function Through Ribbon Windows 8 Pass Arguments to Function Through Ribbon Office 2013
Novice
Pass Arguments to Function Through Ribbon
 
Join Date: Mar 2015
Posts: 8
inagalaxyfarfarawry is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 03-14-2015, 11:18 AM
gmaxey gmaxey is online now Pass Arguments to Function Through Ribbon Windows 7 32bit Pass Arguments to Function Through Ribbon Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 03-14-2015, 04:40 PM
inagalaxyfarfarawry inagalaxyfarfarawry is offline Pass Arguments to Function Through Ribbon Windows 8 Pass Arguments to Function Through Ribbon Office 2013
Novice
Pass Arguments to Function Through Ribbon
 
Join Date: Mar 2015
Posts: 8
inagalaxyfarfarawry is on a distinguished road
Default

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:
Originally Posted by inagalaxyfarfarawry View Post
But how do I pass the function an argument through Word's ribbon?
Quote:
Originally Posted by inagalaxyfarfarawry View Post
Is there a way to do this in MS Word 2010?
Reply With Quote
  #5  
Old 03-14-2015, 05:04 PM
gmaxey gmaxey is online now Pass Arguments to Function Through Ribbon Windows 7 32bit Pass Arguments to Function Through Ribbon Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 03-14-2015, 09:21 PM
inagalaxyfarfarawry inagalaxyfarfarawry is offline Pass Arguments to Function Through Ribbon Windows 8 Pass Arguments to Function Through Ribbon Office 2013
Novice
Pass Arguments to Function Through Ribbon
 
Join Date: Mar 2015
Posts: 8
inagalaxyfarfarawry is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 03-15-2015, 06:13 AM
gmaxey gmaxey is online now Pass Arguments to Function Through Ribbon Windows 7 32bit Pass Arguments to Function Through Ribbon Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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>
This is the code in a VB module named modRibbonControl:

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
Here is the code for a standard module named modMain
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
If you are trying to do this with the cheesy ribbon editor built-in to Word then I can only say, abandon hope all who go there.

See: http://gregmaxey.com/word_tip_pages/...bbon_main.html
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 03-15-2015, 03:48 PM
inagalaxyfarfarawry inagalaxyfarfarawry is offline Pass Arguments to Function Through Ribbon Windows 8 Pass Arguments to Function Through Ribbon Office 2013
Novice
Pass Arguments to Function Through Ribbon
 
Join Date: Mar 2015
Posts: 8
inagalaxyfarfarawry is on a distinguished road
Default

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:
<mso:button idQ="x1:Open_1_0_9D09926" label="(01) Serial Fiction Sideshow" imageMso="AppointmentColor0" onAction="Open_1" visible="true"/>
And so I replace Open_1 with modRibbonControl.ButtonOnAction?
Reply With Quote
  #9  
Old 03-15-2015, 04:01 PM
gmaxey gmaxey is online now Pass Arguments to Function Through Ribbon Windows 7 32bit Pass Arguments to Function Through Ribbon Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

It looks like you are trying to do this with the built-in ribbon editor. I told you already that won't work.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 03-25-2022, 07:44 AM
VBAadvocate VBAadvocate is offline Pass Arguments to Function Through Ribbon Windows 10 Pass Arguments to Function Through Ribbon Office 2019
Novice
 
Join Date: Feb 2022
Posts: 12
VBAadvocate is on a distinguished road
Default 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>
Here are my callbacks:

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"
Reply With Quote
  #11  
Old 03-25-2022, 09:22 PM
Charles Kenyon Charles Kenyon is online now Pass Arguments to Function Through Ribbon Windows 10 Pass Arguments to Function Through Ribbon Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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.
Reply With Quote
  #12  
Old 03-25-2022, 09:28 PM
Charles Kenyon Charles Kenyon is online now Pass Arguments to Function Through Ribbon Windows 10 Pass Arguments to Function Through Ribbon Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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.
Reply With Quote
Reply

Tags
macro, microsoft word 2010, ribbon



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass Arguments to Function Through Ribbon How to Pass Arguments in a Formula? tinfanide Excel Programming 2 10-13-2014 06:10 AM
Pass Arguments to Function Through Ribbon 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:03 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft