Tuesday, February 10, 2009

Special Characters in the Office 2007 Ribbon

Have you ever run into a teeny little problem that was incredibly annoying and almost impossible to debug? Well, I did. All I wanted to do was create a Ribbon Tab at runtime from a database for a Word 2007 Add-In.

Creating the tab and adding the controls was easy. Very easy actually. However, using"special characters" such as "'/" and "(" or ")" in the labels caused the entire tab to not be displayed. No error message, no log, just poof - ribbon gone. Sweet. I tried escaping the characters xml style, but that caused even more problems. I created a ribbon using the Visual Studio 2008 Ribbon Designer. The code I had written looked almost exactly like the code there. Well crap. I Googled the living snot out of the problem and couldn't find a damn thing. I asked team members. I stared at the ceiling. I mediated about it. I even programmed myself to dream about it. Actually, I gave up at the ceiling staring stage, but you get the idea.

Well, today I was looking through the designer code for the ribbon, and it had calls to ribbon.SuspendLayout(), ribbon.ResumeLayout(False) around the calls to add the ribbon controls. I didn't think this would do anything, but hey - why not give it go? Sure enough, that fixed my problems.

So, in summary, do this:

ribbon.SuspendLayout()

... 'Add a bunch of tabs, groups, controls, etc

ribbon.ResumeLayout(False)
and everything will be fine.