I need my custom ui ribbon tab to refresh on changing between documents in Word

Hi experts

I have built a custom ribbon tab in Word using the Custom UI Editor. The custom ribbon tab belongs to a template, so several documents can be open at the same time, all linked to the template.

In my custom ui code I am getting the labels for some of the buttons. They should change depending certain content in the document.

What I've noticed though is that if I have two documents open and I flick between the documents, the ribbon tab is not updating (it stays put, and is reflective of whatever the last document I opened via File > Open).

Is there a way to have the ribbon refreshing depending on what document is active?

this is the first line of the ribbon code.
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="RibbonLoaded">

Open in new window


This is an example of me calling to get the label.
<button id="BtnH1" getLabel="RibbonControls.getLabel" onAction="RibbonControls.ButtonOnAction" />

Open in new window

Fi69Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:
Can you advise where your code is located ... i.e. in a template in the startup or elsewhere?

Chris
0
Fi69Author Commented:
Code is within the template itself.
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Question was meant to elcit if the documents are created from a template or if the template is there for all files .. i.e. is your menu structure created afresh with every new document using the template or does word itself have access to the template whenever opened via the .dotm

Chris
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Fi69Author Commented:
The template contains the VBA code and the custom ui code. It is stored in the Workgroup template directory, not startup directory. Is that what you need?

So when you create a document, or open the document based on the template, it loads the ribbon with the relevant content from that document, but what I'm noticing is that if I then flick into another document I already have opened, the ribbon on that document has also changed (well, probably I should say - it has not changed when I've made that document active).

Is there a way to reload the ribbon when the document becomes the active document? Document_Open doesn't work, because the document is already open and there doesn't look to be a procedure for Document_Activate.
0
Fi69Author Commented:
Oh, and just to clarify the file is a .dotm.
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Just to be clear I have done this myself albeit using the folder the mechanism is the same.

I'm not at work this work to refresh my mind butI will address it next week if we dont get there earlier.

If i'm not mistaken then you need a document event handler in your template and when the change event occurs you enable / disable the selections as appropriate via the ribbon elements visible property.  I haven't tried invalidating the entire ribbon ... this assuming every slection in your menu is visible for every document i.e. you simply change some displayed text then invalidating the ribbon when you change documents may work from the application event handler.

Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fi69Author Commented:
Do you know if an application event handler has to be in an Add-In or can it be in an individual template?
0
Chris BottomleySoftware Quality Lead EngineerCommented:
'Mine' is in the template document so its in the startup directory in the same dotm that has the ribbon code.

Chris
0
Chris BottomleySoftware Quality Lead EngineerCommented:
For example:

'	Alt + F11 to open VBE
'	Insert | Class Module
'	In Properties window rename as thisApplication
'	In the module type/Ensure two lines as:
	Option Explicit
	Public WithEvents wdApp As Word.Application
'	Insert | Module
'	In the module type/Ensure two lines as:
	Option Explicit
	Dim wdAppClass As New thisApplication
	Now type three lines as:
	Public Sub autoexec()
	    Set wdAppClass.wdApp = Word.Application
	End Sub
'	Put the cursor into the autoexec sub and press F5
'	Switch back to the CLASS module inserted in step 3
'	From the drop down select wdApp and the event that you require i.e:

	Private Sub wdApp_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
            refresh_CRB_Menus
	End Sub

Open in new window


Sub OpenCRBMenus()
Dim crbMenu As CommandBarControl
Dim str As String

    With ActiveDocument
        On Error Resume Next
        str = TypeName(CommandBars("Menu Bar").Controls("Chris"))
        Do While str = "CommandBarPopup"
            CommandBars("Menu Bar").Controls("Chris").Delete
            str = ""
            str = TypeName(CommandBars("Menu Bar").Controls("Chris"))
        Loop
        On Error GoTo 0
        With CommandBars("Menu Bar").Controls.Add(Type:=msoControlPopup)
            .Caption = "Chris"
            .Visible = True
            .Tag = "CRB Disposable"
            With .Controls.Add(Type:=msoControlPopup)
                .Caption = "General"
                .Visible = True
                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Open MetaData Form"
                    .OnAction = "callMetaDataForm"
                    .Enabled = True
                End With
                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Refresh CRB Menus"
                    .OnAction = "Refresh_CRB_Menus"
                    .Enabled = True
                End With
                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Delete CRB Menus"
                    .OnAction = "kill_CRB_Menus"
                    .Enabled = True
                End With
                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Refresh Automarks"
                    .OnAction = "refreshAutoMarking"
                    .Enabled = True
                End With
                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Delete Automarks"
                    .OnAction = "DeleteIndexEntries"
                    .Enabled = True
                End With
            End With
        End With
    End With
    
End Sub

Sub refresh_CRB_Menus()
    kill_CRB_Menus
    OpenCRBMenus
End Sub

Sub kill_CRB_Menus()
Dim ctrl As Object
Dim str As String

    str = getUserProp(ActiveDocument, "Meta_Menu_Text", "Default_Menu")
    For Each ctrl In CommandBars("menu bar").Controls
        If ctrl.Tag = "CRB Disposable" Then
            ctrl.Delete
        End If
    Next

End Sub

Sub kill_CRB_Menus_Mega()
Dim ctrl As Object
Dim yesNo As Variant

    For Each ctrl In CommandBars("menu bar").Controls
        If Not ctrl.BuiltIn Then
            Stop
            If ctrl.Caption = "Acrobat &Comments" Then
                yesNo = vbNo
            ElseIf ctrl.Caption = "Ado&be PDF" Then
                yesNo = vbNo
            Else
                yesNo = MsgBox("Delete Control << " & UCase(ctrl.Caption) & " >> ?", vbYesNo)
            End If
            If yesNo = vbYes Then
                ctrl.Delete
                yesNo = ""
            End If
        End If
    Next
End Sub

Open in new window


Copied from an old file I have to hand and this version replaces the whole document BUT as long as any document specific references point to the active document then I would imagine it will update fine.  Obviously you need to replace the menus and objects thereon to suit your application but this should give you a leg up!

Chris
0
Fi69Author Commented:
I just discovered something really interesting - 2010 is refreshing the ribbon, but 2007 isn't. Some of them are on 2007 so I shall create the application event handler. This company has a global Word template, so I'll add it into that file. Thanks for your help!
0
Fi69Author Commented:
Application Event Handler resolves problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.