Changing the properties of elements in Office Ribbon using VBA

懵懂的女人 提交于 2019-12-04 05:23:20

Here is a simple, functional example. I substitute simple InputBox prompts instead of trying to recreate your UserForm code, but the general principal should still apply.

I create public variables for username and fullname and bAuthenticated (have your form assign to these variables, or reference the form directly). Set bAuthenticated = True once you have authenticated the user.

Use the getLabel callback and refresh the ribbon. I create two separate callbacks, one for username, one for fullname, these are getUserName and getFullName. I also add the vba for VisibleGroup which is called from the getVisible XML property.

Note when making changes to your VBA you will likely have to save, close & re-open the file, because making these changes may clear all public variables, including the variable which represents the ribbon itself. This is why you might be getting errors from the RefreshRibbon procedure.

Your xml might look like this.

Updated to include the getVisible callback

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="RibbonOnLoad">
    <ribbon startFromScratch="false" >
        <tabs>
            <tab id="customTab" label="Custom Tab">
                      <group id="myGroup" label="Hello World" getVisible="VisibleGroup" >
                          <labelControl id="lblUsername" getLabel="getUserName" />
                          <labelControl id="lblFullname" getLabel="getFullName" />
                      </group>
                      <group id="mySignin" label="SignIn" visible="true" >
                          <button id="signin" label="Sign In" size="large"
                              supertip="Click this button to sign in."
                              onAction="ribbon_SignIn" tag="SignIn" />
                      </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

Note: I have set the visible property of myGroup to True so that I could more easily test this. You can use the getVisible callback similarly, if you need to change this property at run-time.

And then the callbacks in VBA module will look more or less like:

Option Explicit
Public Rib As IRibbonUI
Public xmlID As String
Public username As String
Public fullName As String
Public bAuthenticated As Boolean

'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
    'MsgBox "onLoad"
    Set Rib = ribbon
End Sub

Sub ribbon_SignIn(control As IRibbonControl)

    username = InputBox("enter your username", "Your username?")
    fullName = InputBox("enter your full name", "Your full name?")

    'Authenticate
    bAuthenticated = True

    VisibleGroup control, bAuthenticated
End Sub
Sub VisibleGroup(control As IRibbonControl, ByRef returnedVal)
    returnedVal = bAuthenticated
    Call RefreshRibbon("myGroup")
End Sub
Sub getUserName(control As IRibbonControl, ByRef returnedVal)
    returnedVal = username
    Call RefreshRibbon(control.id)
End Sub
Sub getFullName(control As IRibbonControl, ByRef returnedVal)
    returnedVal = fullName
    Call RefreshRibbon(control.id)
End Sub
Sub RefreshRibbon(id As String)
    xmlID = id
    'MsgBox "Refreshing ribbon for " & Id, vbInformation
    If Rib Is Nothing Then
        MsgBox "Error, Save/Restart your Presentation"
    Else
        Rib.Invalidate
    End If
End Sub

You can use something called the getLabel property in your xml.

e.g.

getLabel = "GetLabelMacro"

Now in your GetLabelMacro you can put in code as

Sub GetLabelMacro(control As IRibbonControl, ByRef label)
    if control.id = "MyLabel" then
        label = "New Label"
    end if
End Sub

You can modify this sample code for your purposes. More info on the syntax of getLabel can be found by googling about the getLabel callback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!