使用VBA更改Office功能区中元素的属性 [英] Changing the properties of elements in Office Ribbon using VBA
问题描述
我是VBA和Office Ribbon UI的初学者。我正在使用Office 2007,并使用自定义UI在PPTM中开发功能区。我添加了一组XML,如下所示:
I am a beginner in VBA and Office Ribbon UI. I am using Office 2007 and used Custom UI to develop a Ribbon in PPTM. I have added a set of XML like the one below:
<group id="myGroup" label="Hello World" visible="false">
<labelControl id="lblUsername" label="Your Username: " />
<labelControl id="lblFullname" label="" />
</group>
因此,在此 Hello World
标签中,我想将其可见性更改为 true
并更改 lblUsername
和 lblFullname的值
。当前,这必须在上一次调用之后通过此按钮完成:
So, in this Hello World
tab, I would like to change its visibility to true
and change the the values of lblUsername
and lblFullname
. Currently this has to be done after the previous call by this button:
<button id="signin" label="Sign In" image="signin" size="large"
supertip="Click this button to sign in."
onAction="ribbon_SignIn" tag="SignIn" />
现在, ribbon_SignIn
中的代码为
Sub ribbon_SignIn()
SignIn.Show
End Sub
这将打开 SignIn
表单并从用户获取用户名和密码。验证用户名和密码后,一切正常,但是我不确定要获取控件 lblUsername
和 lblFullname
以使用登录的用户的详细信息来更改其值。
This opens the SignIn
form and gets the Username and Password from the user. After the username and password are validated, everything goes fine, but I am not sure what is the procedure to get the properties of controls lblUsername
and lblFullname
to change their values with the signed in user's details.
在 SignIn
表单中,我具有以下 Sign In 按钮的代码。
In the SignIn
form I have the below code for the Sign In button.
Private Sub btnSignIn_Click()
' Authentication Mechanism
MsgBox "You have successfully signed in!"
' Show the Ribbon group.
' What am I supposed to do here to make the group visible?
' Also how do I change the text of the label?
End Sub
那么,在这里我应该放些什么使该组可见?还有如何更改标签的文本?
So, here what should I put to make the group visible? Also how do I change the text of the label?
当我在自定义UI中使用两个属性 getVisible
和 getLabel
时,加载项本身没有得到显示。 :(我使用的代码是:
When I use the two attributes getVisible
and getLabel
in the Custom UI, the add-in itself is not getting displayed. :(
The code that I used was:
<group id="myGroup" label="Hello World" getVisible="VisibleGroup">
<labelControl id="lblUsername" label="Your Username: " getLabel="lblUsername" />
<labelControl id="lblFullname" label="" getLabel="lblFullname" />
</group>
如果删除这两个属性,Weird。BTW,我正在使用 Office 2007 。
If I remove those two attributes, Weird. BTW, I am using Office 2007.
推荐答案
这是一个简单的功能示例。我用简单的 InputBox
提示代替尝试重新创建UserForm代码,但一般原理仍然适用。
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.
p>
我为用户名和全名以及bAuthenticated创建公共变量(将表单分配给这些变量,或者直接引用表单)。设置 bAuthenticated = True
对用户进行身份验证之后。
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.
使用 getLabel
回调并刷新丝带。我创建了两个单独的回调,一个用于用户名,一个用于全名,分别是 getUserName
和 getFullName
。我还为 VisibleGroup
添加了vba,它是从 getVisible
XML属性调用的。
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.
注意,对VBA进行更改时,您可能必须保存,关闭并删除重新打开文件,因为进行这些更改可能会清除所有公共变量,包括代表功能区本身的变量。这就是为什么您可能会从 RefreshRibbon
过程中得到错误的原因。
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.
您的xml可能看起来像这样。
Your xml might look like this.
已更新为包括getVisible回调
<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>
注意:我已经设置了 myGroup 更改为 True
,这样我可以更轻松地对此进行测试。如果需要在运行时更改此属性,则可以类似地使用 getVisible
回调。
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.
然后VBA模块中的回调看起来或多或少像这样:
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
这篇关于使用VBA更改Office功能区中元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!