Word VSTO - Document Changed Event - vb.net

we are using the Application.DocumentChange event so that when a document is loaded, it can check the name of the document, and then if it is named in a certain way show or hide buttons on the ribbon.
If I use the code below it works really well, it shows or hides the buttons correctly.
But when I run the addin in debug mode from Visual Studio, when the document is closing it gives this message.
System.Runtime.InteropServices.COMException: 'This command is not available because no document is open.'
As you can see I tried to put in an if statement to stop it running the code but it didnt work.
If I just install my addin, Word does not seem to crash or have any problems, maybe I shouldnt worry about it?
Thanks
Private Sub Application_DocumentChange() Handles Application.DocumentChange
If Globals.ThisAddIn.Application.Documents.Count > 0 Then
If Globals.ThisAddIn.Application.ActiveDocument.BuiltInDocumentProperties("Title").Value = "Document Name Here" Then
Globals.Ribbons.VisualfilesTab.AttachEvidence.Visible = True
Else
Globals.Ribbons.VisualfilesTab.GetAuthorisation.Visible = True
End If
End If
End Sub

Consider switching your custom UI to the ribbon XML over the designer-generated custom ribbon UI. You can export your existing custom UI to the ribbon XML markup. See How to: Export a ribbon from the Ribbon Designer to Ribbon XML for more information.
You can customize the Ribbon UI by using callback procedures in COM add-ins. For each of the callbacks that the add-in implements, the responses are cached.
For example, if an add-in writer implements the getVisible callback procedure for a button, the function is called once, the state is loaded, and then if the visibility state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate or IRibbonUI.InvalidateControl method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.
Read more about the the Fluent UI (aka Ribbon UI) in the following articles:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

Related

MS Word lock/unlock Quick Access Toolbar button with VBA

I have two buttons which I've added to the Quick Access Toolbar (QAT) in MS Word, both of which are linked to macros written in a module. I want the second button to be greyed out (or locked) until the first one has successfully run, at which point I will unlock in the code.
How do I access the button objects with vba? I'm looking for something along the lines of ActiveDocument.QATButton1.Locked = True. With a standard Active X button you can right click and view properties to get the button name but I can't seem to do this with these (they were created via Options>QAT>Macros>Add)
If you want to disable or enable your Fluent UI controls (QAT) dynamically you need to use ribbon callbacks with IRibbonUI.Invalidate or IRibbonUI.InvalidateControl methods. For each of the callbacks that the custom UI implements, the responses are cached.
For example, if an add-in writer implements the getEnabled callback procedure for a button, the function is called once, the state loads, and then if the state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.
You may find the Ribbon UI (aka Fluent UI) described in depth in the following series of articles:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

synchronize Outlook Ribbon togglebutton with registry settings in VB.Net for an Add-In

I am building an Outlook Add-In in Visual Basic and I have a togglebutton in the ribbon that controls whether a setting is on or off. This settings is saved and retrieved using "GetSettings" and "SaveSetting" which saves information to the Windows Registry (to my understanding). I want the togglebutton the correspond to changes made to that setting as it can be changed in other places than just the togglebutton. My problem would be solved if I could access the value of the togglebutton and setting it manually but as I have understood it: I cannot access it as I am building my ribbon in XML. I have not been able to find a solution to this, can anyone help?
Side note: If I add my togglebutton to the taskbar in Outlook and from there toggle the settings, the togglebutton in the ribbon doesn't change to "pressed" mode. Perhaps this is something that can be fixed with any provided solution from anyone :)
You need to use IRibbonUI.Invalidate or for a single control you may use the IRibbonUI.InvalidateControl method to get your ribbon callbacks invoked for updating the cached values according to the state loaded from settings.
For example, if an add-in implements the getPressed callback procedure for a toggle button, the function is called once, the state loads, and then if the state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method (VSTO).
You can read more about the Fluent UI (aka Ribbon UI) in the following articles:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
Do not rely on the button to store the state for you - whenever it changes, it is your responsibility to refresh the Ribbon (by calling IRibbonUI.Invalidate), which will cause Outlook to invoke your callback on the toggle button to determine its state. You just need to store the appropriate state in your code.

When Ribbon button is clicked disable multiple buttons

I have a couple of button is an MS access application that need to be enabled or disabled at the same time. When one of them is clicked and opens a form they both need to be set to disabled. When the form is closed they both need to be set to enabled.
I have no problem doing this for the control that is clicked. But I don't know how to reference the one that was not clicked.
Here is the code for my ribbon:
and the funciton I use to handle getEnabled:
How do I disabled or enable both buttons at the same time?
You need to use the IRibbonUI.Invalidate or IRibbonUI.InvalidateControl methods to get your callback invoked for other controls. The invalidate method allows invalidating the cached values for all of the controls of the Ribbon user interface.
For example, if an add-in writer implements the getImage callback procedure for a button, the function is called once, the image loads, and then if the image needs to be updated, the cached image is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.
Dim MyRibbon As IRibbonUI
Sub MyAddInInitialize(Ribbon As IRibbonUI)
Set MyRibbon = Ribbon
End Sub
Sub myFunction()
MyRibbon.Invalidate() ' Invalidates the caches of all of this add-in's controls
End Sub
The Fluent UI (aka Ribbon UI) is described in depth in the following series of articles:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

Is getLabel safe to use (VBA, PowerPoint)?

I would like to use a getLabel callback from some custom XML in a PowerPoint ppam in order to localise text in the Ribbon. My concern is that doing this could cause error messages to appear from time to time when starting PowerPoint ("PowerPoint can't start this feature because you currently have a Visual Basic for Applications project in break mode.") as users have experienced with an add-in that uses a getEnabled callback. I have already asked a question about other options for localising the ribbon.
Do you use getLabel in a PowerPoint ppam add-in? If so, do you sometimes see this error message when PowerPoint is started? Additionally, do you use getEnabled, and does this ever cause this error message to be displayed when PowerPoint starts up?
Answers either way would be great.
This is what I am testing:
Custom UI XML fragment:
<button id="app1ShowAMsg"
imageMso="TableInsert"
size="normal"
onAction="app1ShowAMessage"
label="app1GetLabel"/>
VBA code:
Public Sub app1GetLabel(control As IRibbonControl, ByRef returnedVal)
Select Case control.Id
Case "app1ShowAMsg"
returnedVal = "My added label"
End Select
End Sub
Public Sub app1ShowAMessage()
MsgBox "You clicked a button."
End Sub
Yes, it is. I don't see anything strange in using the getLabel callback along with others. Moreover, that is the recommended way of implementing any dynamism on the Fluent UI - for changing the caption string and etc. at runtime. The Fluent UI is described in depth in the following series of articles:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
Also you may find the following articles helpful:
Chapter 11: Creating Dynamic Ribbon Customizations (1 of 2)
Chapter 11: Creating Dynamic Ribbon Customizations (2 of 2)

How to make changes in Ribbon elements of a single excel instance, using application-level add-in?

I'm using VSTO with VB.NET for Excel 2013. I'm developing an application-level add-in, but I can't make two different workbooks store different "ribbon state". For example, when I want to enable a button, I use the following code:
Globals.Ribbons.Ribbon1.myButton.Enable = False
This makes the element "myButton" to be disabled on each opened workbook, but I want to make it disabled only for one workbook.
The way I'm doing now is to handle the event WorkbookActivate, to change the ribbon state. The problem is: this way, the user sees an invalid state at other workbooks that are not on the top.
There is some better workaround? There isn't a way to manage the ribbon instances (and not only the global element as I am doing)?
Thanks
There is some better workaround? There isn't a way to manage the ribbon instances (and not only the global element as I am doing)?
You need to use callbacks instead. Try to use the getEnabled callback instead.
Moreover, when required you can use the IRibbonUI interface methods to force Office applications to update the UI controls. The Invalidate method invalidates the cached values for all of the controls of the Ribbon user interface. See Overview of the IRibbonUI Object .
You can read more about the Ribbon UI (aka Fluent UI) in the following series of articles in MSDN:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)