Change item properties in Office Ribbon using VBA

I am new to VBA and Office Ribbon interface. I am using Office 2007 and used the user interface to develop the tape in PPTM. I added an XML set as shown below:

<group id="myGroup" label="Hello World" visible="false"> <labelControl id="lblUsername" label="Your Username: " /> <labelControl id="lblFullname" label="" /> </group> 

So, on this Hello World tab, I would like to change its visibility to true and change the values ​​of lblUsername and lblFullname . Currently, this needs to be done after the previous call with this button:

 <button id="signin" label="Sign In" image="signin" size="large" supertip="Click this button to sign in." onAction="ribbon_SignIn" tag="SignIn" /> 

Currently, the code in ribbon_SignIn as follows:

 Sub ribbon_SignIn() SignIn.Show End Sub 

This opens the SignIn form and receives the username and password from the user. After the username and password are verified, everything will be fine, but I'm not sure if it is a procedure to get the properties of the lblUsername and lblFullname in order to change their values ​​using signed user details.


Explanation

In the SignIn form, I have the code below 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, what should I do to make the group visible? Also how to change label text?


Update # 1

When I use the two getVisible and getLabel in the user interface, the add-in itself does not appear. :( The code 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> 

If I remove these two attributes, Weird. BTW, I am using Office 2007 .

+6
source share
2 answers

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

I create public variables for username and full name and bAuthenticated (assign your form to these variables or directly access the form). Set bAuthenticated = True after user authentication.

Use the getLabel and update the feed. I create two separate callbacks, one for the username, one for the full name, these are getUserName and getFullName . I also add vba to VisibleGroup , which is called from the getVisible XML getVisible .

Note when making changes to VBA, you will most likely have to save, close and reopen the file, because making these changes can clear all public variables, including the variable, which is a tape. That's why you can get errors from the RefreshRibbon procedure.

Your xml might look like this.

Updated to enable 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 set the visible property of myGroup to True so that I can more easily verify this. You can use the getVisible same way if you need to change this property at runtime.

And then the callbacks in the VBA module will look something like this:

 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 
+3
source

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

eg.

 getLabel = "GetLabelMacro" 

Now in your GetLabelMacro you can enter the code as

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

You can change this sample code for your purposes. Additional getLabel syntax information can be found with googling about getLabel callbacks.

+4
source

Source: https://habr.com/ru/post/971868/


All Articles