VSTO 3.5 Word Add-In - ribbon XML does not refresh

S

SMutlu

I'm having trouble getting my custom buttons to be visible or not visible
based on the active document's template. I've set the getVisible method for
each group of buttons in the XML file to a function that checks the
document's template and if the name is the same as the XML group id then I do
a ribbon.InvalidateControl(group.id) and the function returns True which
should make the group visible. When I test this by opening documents based
on different templates it works for the first few documents but not
subsequent ones. My code and XML is this:-

Public Function IsCustomGroupVisible(ByVal group As Office.IRibbonControl)
As Boolean
Return WhichTemplate(TryCast(group.Context, Word.Window), group)
End Function

Private Function WhichTemplate(ByVal Window As Word.Window, ByVal group As
Office.IRibbonControl) As Boolean

If Window Is Nothing Then
Return False
Else
Dim doc As Word.Document = DirectCast(Window.Parent,
Word.Document)
Dim docTemplate = CType(doc.AttachedTemplate, Word.Template)

'get just the template name minus the extension
Dim iValue As Integer = InStr(docTemplate.Name, ".",
CompareMethod.Text)
Dim strTemplate As String = Mid(docTemplate.Name, 1, iValue - 1)
Dim iStrCompare As Integer

iStrCompare = StrComp(strTemplate, group.Id, CompareMethod.Text)

MsgBox("Document Template is " & strTemplate & vbNewLine &
"Ribbon Group is " & group.Id _
& vbNewLine & "Document name is " & doc.Name)

If iStrCompare = 0 Then
Try
ribbon.InvalidateControl(group.Id)
Return True
Catch ex As Exception
MsgBox(ex)

End Try

Else
Return False
End If
End If
End Function

<group id="Letter" label="Letter" getVisible="IsCustomGroupVisible">
<button id="btnCopiedTo"
imageMso="CopyToPersonalContacts"
onAction="btnCopiedTo_Click"
screentip="Add ccs/bccs"
supertip="Add people to copy this letter to"
label="Add copies"
size="large" />

<button id="btnSendAsFax"
imageMso="FileInternetFax"
onAction="btnSendAsFax_Click"
screentip="Send as a fax"
supertip="Send this letter as a fax"
label="Send as a fax"
size="large" />
</group>

<group id="Fax" label="Fax" getVisible="IsCustomGroupVisible">
<button id="btnCopyFaxTo"
imageMso="Copy"
onAction="btnCopyFaxTo_Click"
screentip="Copy fax to another party"
supertip="Add people to copy this fax to"
label="Add copies"
size="large" />
</group>
 
C

Cindy M.

Hi =?Utf-8?B?U011dGx1?=,
I'm having trouble getting my custom buttons to be visible or not visible
based on the active document's template. I've set the getVisible method for
each group of buttons in the XML file to a function that checks the
document's template and if the name is the same as the XML group id then I do
a ribbon.InvalidateControl(group.id) and the function returns True which
should make the group visible. When I test this by opening documents based
on different templates it works for the first few documents but not
subsequent ones.
I think the key may be whatever should be triggering InvalidateControl?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
S

SMutlu

Hi Cindy

Thanks for replying. This XML - <group id="Letter" label="Letter"
getVisible="IsCustomGroupVisible"> triggers the IsCustomGroupVisible function
which calls the WhichTemplate function which in turn returns True or False to
the IsCustomGroupVisible function. The times the group does not display when
it should, the function IsCustomGroupVisible is not triggered and I'm not
sure what triggers the getVisible element.
 
C

Cindy M.

Hi =?Utf-8?B?U011dGx1?=,
The times the group does not display when
it should, the function IsCustomGroupVisible is not triggered and I'm not
sure what triggers the getVisible element.
A getVisible callback will only be triggered if the Ribbon.Invalidate or the
Ribbon.InvalidateControl methods are invoked in your code. Nothing else
should affect this.

So I ask again:

<<When I test this by opening documents based
on different templates it works for the first few documents but not
subsequent ones. >>

What are you using to invoke the Ribbon invalidation methods? The
DocumentOpen event? Also the NewDocument event? Something else?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

SMutlu

Hi Cindy

Thanks for the reply.

I have customUI onLoad="Ribbon_Load" in my XML file with the callback of:-

Public Sub Ribbon_Load(ByVal ribbonUI As Office.IRibbonUI)
Me.ribbon = ribbonUI
End Sub

This loads the XML file and sets the getVisible state of each of my custom
groups to false which is correct because when you launch Word you get a blank
document based on Normal and I don't have a custom group relating to this
template.

I don't have any defined document events in my AddIn. The act of opening a
document appears to trigger the getVisible method because my custom tab's
insertBeforeMSO is set to "TabHome". Before I had this set to
insertAfterMso="TabHome" and then when I clicked on my custom tab the
getVisible methods were triggered.

Thanks for your help in this.
 
C

Cindy M.

Hi =?Utf-8?B?U011dGx1?=,
I don't have any defined document events in my AddIn.
In that case, try hooking up the application's DocumentOpen
event and invalidate the Ribbon in that. See if it makes a
difference.

In order to access the Ribbon class of your Add-in use the
Globals keyword. Roughly, it would be like this
Globals.MyRibbonClass.Ribbon.Invalidate()

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
V

Virtualware

Hi, I'm not suer if you solve the issue of the subsequent refresh
I'm using MS-Access and have a similar problem.

the way I solve it was as shown below:
Public gobjRibbon As IRibbonUI
Public gobjRibbon1 As IRibbonUI

Public Sub CallbackOnLoad(ribbon As IRibbonUI)
' Cache a copy RibbonUI
Set gobjRibbon = ribbon
End Sub

Public Sub CallbackOnLoad1(ribbon As IRibbonUI)
' Cache a copy RibbonUI
Set gobjRibbon1 = ribbon
End Sub

then I just called from the xmlcode CallbackOnLoad1
 
S

SMutlu

With the help of Microsoft Support the following workaround was done similar
to your workaround.

1. Declared public variable in ThisAddIn:
Public m_ribbon As Ribbon

2. Changed the CreateRibbonExtensibilityObject function to:
m_ribbon = New Ribbon()
Return m_ribbon

3. Changed the scope of the variable ribbon as Office.IRibbonU to public
4. Implemented Application_DocumentOpen event to invalidate control:

Private Sub Application_DocumentOpen(ByVal Doc As
Microsoft.Office.Interop.Word.Document) Handles Application.DocumentOpen
m_ribbon.ribbon.InvalidateControl("rxBtnCheckDocument")
End Sub

Ribbon now works for every document that is opened.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top