RibbonCustomizer™
Customize your Office 2007 Ribbon (Office Fluent™)with only a few mouse clicks! Works with Microsoft® Access™, Excel®, Outlook®, PowerPoint® and Word 2007.

Buy for $29.99

Download free Starter Edition

Subscribe | Subscribe by Email |

Categories

Archive


RibbonX Callbacks

July 27th, 2006 by Patrick Schmid

Callbacks are a fundamental feature of RibbonX. It is therefore important for developers to understand when they are executed and when they may fail. As a principal rule, callbacks shouldn’t assume anything and should protect themselves against all potential failures. Let me illustrate some of the difficulties with the following example.

Revisiting the StyleChooser add-in

V1.2.1 of my StyleChooser add-in contained a callback that failed. To reproduce the error, you can download V1.2.1 and its source code. Install it and open Word. Before you continue, make sure that UI errors of add-ins are being displayed (Word Options, Advanced, General section, Show add-in user interface errors). With the Home tab displayed, switch to the Full Screen Reading mode via the status bar. In that mode, click on Print Layout. You should then see a CustomUI error message: An exception occured while calling function “GetText”. The exception message is: This method or property is not available because typing is not currently allowed for reading.

What happened? GetText looks like this:

Public Function GetText(ByVal control As IRibbonControl) As String

If control.Id = “comboStyleChooser” Then

Dim selection As Microsoft.Office.Interop.Word.Selection = DirectCast(applicationObject.Selection, Microsoft.Office.Interop.Word.Selection)

Return formattedStyle(DirectCast(selection.Style, Microsoft.Office.Interop.Word.Style))

End If

Return “”

End Function

The error is caused by the call selection.Style as Word is still in the Full Screen Reading mode when the callback is being executed. In that mode, this particular call is not allowed. I fixed this problem with V1.2.2 by wrapping the call in a try-catch statement (download source code). I assumed this to be a bug in Word 2007 and submitted it to Microsoft as such. Unfortunately, Microsoft couldn’t fix the issue as executing callbacks when the ribbon was already displayed caused some serious flickering.

Don’t assume anything

Therefore add-in developers need to ensure that callbacks don’t make any assumptions. A callback cannot assume that a particular document is open or that the application is in a particular mode:

Callbacks might be executed before the user interface or any particular document is displayed or available via the object model.

Callbacks should not assume anything and should be ready to be called at any time. They therefore need to check for various conditions to prevent errors like the one in my StyleChooser 1.2.1 version. This applies to all RibbonX callbacks, not just to those implemented by COM add-ins.

Comments are closed.