动态更改功能区的按钮标签Excel

时间:2022-12-28 02:28:09

I am using the following piece of XML code to create a custom Ribbon for an Excel add-in.

我使用以下XML代码为Excel加载项创建自定义功能区。

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon startFromScratch="false">
        <tabs>
            <tab id="ComdinheiroTab" label="COMDINHEIRO">
                <group id="ComdinheiroButtons" label="Comdinheiro">

                    <button id="Login" getLabel="getLabelLogin" image="Login" size="large" onAction="OnActionLogin"/>

                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

I am using the following VBA code to set a label for the button login:

我使用以下VBA代码为按钮登录设置标签:

Sub getLabelLogin(control As IRibbonControl, ByRef returnedVal)
 if loggedIn = true then
    returnedVal = "Logged"
 else 
    returnedVal = "Disconected"
 end if
End Sub

The label's name changes successfully according to the variable loggedIn's value when the ribbon is loaded. However I wish I could change the label's values during the execution of my program. Is it possible to call getLabel event using a VB code? Is there anyway to refresh my ribbon so this event would be called again?

加载功能区时,标签的名称会根据变量loggedIn的值成功更改。但是我希望我可以在执行程序时更改标签的值。是否可以使用VB代码调用getLabel事件?反正有没有刷新我的功能区,所以这个事件会被再次调用?

1 个解决方案

#1


9  

Yes, it's possible to run "get" callbacks later on. In order to do so, you need to create a module- or global-level variable to hold the "ribbon UI" object. That object has two useful methods: Invalidate and InvalidateControl. The first triggers all the "get" callbacks in the Ribbon XML. The second triggers the callbacks only for the specified control.

是的,稍后可以运行“get”回调。为此,您需要创建一个模块或全局级变量来保存“功能区UI”对象。该对象有两个有用的方法:Invalidate和InvalidateControl。第一个触发Ribbon XML中的所有“get”回调。第二个触发器仅针对指定的控件触发回调。

Your ribbon ui must be assigned to this object when the Ribbon loads. In order for this to happen, you need the attribute onLoadin the customUI tag of your Ribbon XML and its callback in your VBA.

在功能区加载时,必须将功能区ui指定给此对象。为了实现这一点,您需要在Ribbon XML的customUI标记及其VBA中的回调中使用属性onLoad。

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="ribbonLoaded">
    <ribbon startFromScratch="false">
        <tabs>
            <tab id="ComdinheiroTab" label="COMDINHEIRO">
                <group id="ComdinheiroButtons" label="Comdinheiro">

                    <button id="Login" getLabel="getLabelLogin" image="Login" size="large" onAction="OnActionLogin"/>

                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

The VBA:

VBA:

Dim ribbonUI as IRibbonUI

Sub ribbonLoaded(ribbon as IRibbonUI)
  Set ribbonUI = ribbon
End Sub

Sub UpdateTheLabel
  ribbonUI.InvalidateControl("Login")
End Sub

Sub getLabelLogin(control As IRibbonControl, ByRef returnedVal)
 if loggedIn = true then
    returnedVal = "Logged"
 else 
    returnedVal = "Disconected"
 end if
End Sub

It doesn't matter what procedure calls InvalidateControl as long as the procedure has access to the ribbonUI object.

只要过程可以访问ribbonUI对象,任何过程调用InvalidateControl都无关紧要。

More on this can be found in the MSDN article https://msdn.microsoft.com/en-us/library/aa338202(v=office.12)#OfficeCustomizingRibbonUIforDevelopers_Dynamically

有关这方面的更多信息,请参阅MSDN文章https://msdn.microsoft.com/en-us/library/aa338202(v=office.12)#OfficeCustomizingRibbonUIforDevelopers_Dynamically

#1


9  

Yes, it's possible to run "get" callbacks later on. In order to do so, you need to create a module- or global-level variable to hold the "ribbon UI" object. That object has two useful methods: Invalidate and InvalidateControl. The first triggers all the "get" callbacks in the Ribbon XML. The second triggers the callbacks only for the specified control.

是的,稍后可以运行“get”回调。为此,您需要创建一个模块或全局级变量来保存“功能区UI”对象。该对象有两个有用的方法:Invalidate和InvalidateControl。第一个触发Ribbon XML中的所有“get”回调。第二个触发器仅针对指定的控件触发回调。

Your ribbon ui must be assigned to this object when the Ribbon loads. In order for this to happen, you need the attribute onLoadin the customUI tag of your Ribbon XML and its callback in your VBA.

在功能区加载时,必须将功能区ui指定给此对象。为了实现这一点,您需要在Ribbon XML的customUI标记及其VBA中的回调中使用属性onLoad。

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="ribbonLoaded">
    <ribbon startFromScratch="false">
        <tabs>
            <tab id="ComdinheiroTab" label="COMDINHEIRO">
                <group id="ComdinheiroButtons" label="Comdinheiro">

                    <button id="Login" getLabel="getLabelLogin" image="Login" size="large" onAction="OnActionLogin"/>

                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

The VBA:

VBA:

Dim ribbonUI as IRibbonUI

Sub ribbonLoaded(ribbon as IRibbonUI)
  Set ribbonUI = ribbon
End Sub

Sub UpdateTheLabel
  ribbonUI.InvalidateControl("Login")
End Sub

Sub getLabelLogin(control As IRibbonControl, ByRef returnedVal)
 if loggedIn = true then
    returnedVal = "Logged"
 else 
    returnedVal = "Disconected"
 end if
End Sub

It doesn't matter what procedure calls InvalidateControl as long as the procedure has access to the ribbonUI object.

只要过程可以访问ribbonUI对象,任何过程调用InvalidateControl都无关紧要。

More on this can be found in the MSDN article https://msdn.microsoft.com/en-us/library/aa338202(v=office.12)#OfficeCustomizingRibbonUIforDevelopers_Dynamically

有关这方面的更多信息,请参阅MSDN文章https://msdn.microsoft.com/en-us/library/aa338202(v=office.12)#OfficeCustomizingRibbonUIforDevelopers_Dynamically