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


Developer: “Hello World” in Excel, PowerPoint and Word with VBA (Part 1)

June 2nd, 2006 by Patrick Schmid

Sorry that the post is late. It took me a lot longer to write this than I thought it would. The post today is going to show you how to do the following in Excel, PowerPoint and Word from a document or template:

Screenshot: Hello World button on Home tab

Specifically, I will show the following:

  • How to add RibbonX to a document or template
  • How to add a group with a label to the Home tab using RibbonX
  • How to add a button to this group with a label and icon using RibbonX

In the second part, I am going to show you the free tool Microsoft offers to make your life a lot easier when writing RibbonX for documents and templates. If you know of any other tools that are already out there, please post a comment and I will make sure to include it in my discussion. Part 3 of this will show you how to implement an action in VBA that is triggered when the user presses the “Hello World” button.

Writing RibbonX code

If you remember my overview from the last post, the first step in customizing the Office UI is the RibbonX code itself. For this sample, I am going to use the following code, which works for Excel, PowerPoint and Word:

Hello World RibbonX code
The first line specifies that this is a UI modification for Office 2007 and points to the XML Schema that defines what the syntax of RibbonX has to be (RibbonX XML Schema, sorry that it is not a link to MSDN. I couldn’t find it there, but it should be there) .

The ribbon tag specified that this is a modification of the ribbon. If you have read about RibbonX already, you will know that you can also modify the QAT and repurpose Office commands (having your action triggered when someone uses an MS control) with it. You might have seen in other examples the attribute startFromScratch=”false” used with the ribbon tag. Specifying it as false is not required, because that is the default. Specifying true means that you want to start with a blank ribbon and only have the items on the ribbon specified in your RibbonX code. That setting is only recommended if you truly have a completely different ribbon for your application (the most common use of this is probably with Access applications and Excel Dictator Applications). If you modify the Ribbon only slightly, do not use startFromScratch=”true”.

The following tag (tabs) specified that we are dealing with the main tabs and not contextual tabs. The tag (tab) indicates the actual tab we are working with. The attribute idMso means that we are using an MS-provided tab, in this case the Home tab. If I had specified an attribute id instead, I would have been creating my own tab.

With the next line, I am adding a group to the Home tab. This group has the id “helloWorldGroup” and I gave it the label “Hello World”. Note that labels are optionals, ids (or idMsos or idQs) are not.

The following line adds a button to the group. The button will be large (size attribute), have the id “helloWorldButton”, the label “Hello World” and use the icon the MS-control with the id “Conference” has. If I had written “Paste” instead, I would have been using the image from the MS-Control Paste. To figure out the idMso of all MS controls, either download the control ID list or go into Options, Customization. In that dialog, the tooltip in the list of controls you can add to the QAT will show you the idMso in parenthesis:

Tooltips containing idMso

The remaining lines are the closing tags XML requires (in XML, all tags that were opened need to be closed again in reverse order of opening). Notice that the tag <button … /> is an XML shorthand for <button …></button>. You can use this shorthand if a tag does not contain any other tags.

You can copy & paste this code into Notepad e.g. and then save the file as “customUI.xml” on your Desktop. You can also download the file.

Loading RibbonX code into Word via a document

Now that you have your RibbonX code written, you need to load this into Office. For the first example, I am going to use Word and load the code via a document.

Before you get started with any RibbonX loading, you should activate the display of RibbonX errors: In Word, you can find the settings in Word Options, Advanced, General, “Show add-in user interface errors” (at the very bottom of that long list of settings). It’s called the same and in the same spot for all other Ribbon apps. If you enable it in one program, it will be enabled for all. You absolutely should enable it, because if you don’t, Office will not tell you when you made a mistake in your RibbonX code and you will be wondering why certain things are not displayed or don’t work.

You can only load RibbonX code from a document or template in the new XML-based Office 2007 file formats. If you don’t know anything at all about them, you can find a lot of background information on Brian Jones’s blog. I will explain though the basics you need to make your RibbonX code work.

The new file formats are nothing else than a collection of several files, mostly in XML format, zipped together. I say mostly, because media files (pictures etc) are stored in their original form in the zip container as well. All these files are woven together using the Open Container Format. The short explanation for what that is, is that even though all files are present in the zip container, all files need to be referenced in a tree-like structure. There is one central XML file that contains relationships to the top level XML components. Those in turn have relationship files that link in to the next level components, etc.

To add your RibbonX code to a Word document, all you need to know is that you need to modify the top-level relationship XML file and a relation there to your customUI.xml file. If you just add the customUI.xml file to the zip container without adding that relation, Office will give you an error. Due to a bug, that error might claim that Office has trouble accessing the file itself instead of telling you that the problem is within the file.

To get started, you need a Word document to work with. Open Word, and when you have the default blank file in front of you, click the Office button, Save As (yes, this is a button you can click). As type, choose Word Macro-Enabled Document (*.docm) and save the file on your desktop as helloWord.docm.

Close Word and rename the file into helloWord.zip (if you don’t have extensions displayed by default, open any folder in Windows Explorer, go into Tools, Folder Options and deselect in the View tab “Hide extensions for known file types”). To rename the file, right-click it and select Rename, then replace .docm with .zip. Windows is going to ask you whether you really want to do this, say Yes. If you now click on the file, it will open with your default zip program. If you don’t have a program like WinZip installed, it will open with the Windows built-in zip folder functionality. I am going to use that one in my example (if you have another default zip program, right-click the file, select Open With and select “Compressed (zipped) folder”). You should see the following window then:

opened Zip file

As the first step, you are going to add your XML file to this zip container. We could add the file wherever we wanted, but for this example, let’s add the file to the “word” folder. Click on “word”. Then copy the file customUI.xml from your desktop (right-click, copy) and paste it into this folder (right-click, paste). After that, go back to the top-level folder of the zip file shown in the screenshot above and click on “_rels”. The file “.rels” is the top-level relationship file that we need to edit. Right-click it, say copy and paste it onto your Desktop. Then open it with notepad (open Notepad from All Programs, Accessories, select File, Open, change file type to all files and select the “.rels” file). You will probably see something like this:

If you only see two lines of text, activate Format, Word Wrap.

You should notice that this is not a neatly indented XML file like the RibbonX code I showed above. The reason for that is that XML doesn’t care about whitespace (tabs, enter, spaces). In fact, whitespace reduces the performance when reading an XML file which is why all XML of the new Open XML format avoids whitespace. If you like to see your XML neatly aligned, you should get an XML editor that is able to “pretty print XML”. One such editor is Altova XMLSpy. You can get the Home version for free. The pretty-print feature is in the Edit menu.

I used it to get this pretty-printed version of the .rels file:

.rels file
What we want to do is add a relationship to this file. A relationship needs an Id, which has to be unique. It doesn’t matter though what you pick as Id, as Office will reassign them every time it saves a file anyhow. The important thing is to get Type correct. Type has to be: “Type=”http://schemas.microsoft.com/office/2006/relationships/ui/extensibility”. As Target, you need to set the path to the customUI.xml file. In this case, we specify “word/customUI.xml”. I should note at this point that the filename doesn’t matter either. Word will identify the file using this relationship tag only. The finished Relationship tag looks like this:

<Relationship Id=”customUI” Type=”http://schemas.microsoft.com/office/2006/relationships/ui/extensibility” Target=”word/customUI.xml” />

And the entire .rels file now like this:

.rels file
You can now save the file, copy it from the desktop and paste it back into the “_rels” folder of the zip folder (making sure to overwrite the .rels file there). After that is done, close the zip folder and rename the file back into “helloWorld.docm”. You can download the file as well. You can now open your file in Word and should see the following:

hello World button

It could be the case that Word presents you with an error message when you open the file. If the error message dialog has as its title “Error Loading Custom UI XML” (see the screenshot below for an example), then the error is in your RibbonX code.

RibbonX error message

If you see a different error message, e.g. the following, the error is in the “.rels” file.

.rels file error message

What could you have done possibly wrong? A good guess is that XML is CASE-SENSITIVE, which means if you don’t follow the exact capitalization, you will get that error message. Please remember the following: “Id” has a capital I in the “.rels” file, but “id” is all lower case in RibbonX! (I made that mistake myself first while writing this). Another thing you might want to look at is quotes (”). When a blog post is published from Word, it can very well happen that you end up with curly type quotes instead of the straight ones. You have to make sure that your quotes are the standard straight ones.
Off-topic: Did I mention that blogging in OneNote has one great advantage? When lightning causes a power spike that switches off your desktop while typing, you don’t lose much. In my case, I think I lost 3 characters in total!

Loading RibbonX into Excel, PowerPoint and Word documents and templates

The procedure for loading RibbonX into other file types is exactly the same.

For Word documents use “Word Macro-Enabled Document (*.docm)”

For Word templates use “Word Macro-Enabled Template (*.dotm)”

For Excel documents use “Excel Macro-Enabled Workbook (*.xlsm)”

For Excel templates, use “Excel Macro-Enabled Template (*.xltm)”

For PowerPoint documents, use “PowerPoint Macro-Enabled Presentation (*.pptm)”

For PowerPoint templates, use “PowerPoint Macro-Enabled Template (*.potm)”

The next post will showcase a free tool from Microsoft that inserts RibbonX code for you into Office documents.

Edited to fix the pictures and include the quote remark. Thanks Greg for pointing both things out!

14 Responses to “Developer: “Hello World” in Excel, PowerPoint and Word with VBA (Part 1)”

  1. Greg Maxey Says:

    Patrick,

    I had troubles at first but finally success with phase I. HelloWorld.docm opens with the UI customization as intended.

    I had the same problem my first few tries as I have had all along. However reading your notes make me look harder at the .rel file. I had been copying and pasting from the MSDN article and your article. I found that some of the quote *”* symbols where of the curly type vice straight. I made them all straight and it worked perfectly.

    You may want to add a note about that. Now I am off to see if I can get that button to fire a macro.

    Thanks you. Thank you very much.

  2. Greg Maxey Says:

    Since I did get errors the first few tries this line really threw me:

    You can now open your file in Word and should see the following:

    and then you show two differt error messages.

    It made me think that after making the effort to follow your steps exactly that I was supposed to see one or the other errors ;-)

  3. Patrick Schmid Says:

    Thanks Greg. I fixed both things. It turned out when I was manually putting the images in, I missed one.

  4. Greg Maxey Says:

    Patrick,

    With the knowledge I have gained working through your example, I have gone back and tried to sort out the problem using the example in the MSDN article.

    It has you put the following line in the .rel file:

    I am not sure how that line will show up in your blog, but if it is pasted from the article to the Notepad there is a extra space between the end of relationships and the / which was apparently the cause of all of my woes. Closing up that space made all the difference.

    That said, the trouble was worth it, because if not for it, I would not have been searching for answers and stumbled on your blog. I don’t plan to be a wizard at this stuff but the information and tutorials that you are providing has been the best.

    Keep up the good work.

  5. Patrick Schmid Says:

    Thanks for the compliments and the comment didn’t show up, because of it being interpreted as actual tags. That nasty issue is part of the reason why I showed my code samples as images.

  6. Maurice Says:

    Patrick,

    Well written and well explained. This should do the trick. Looking forward to the next post.

    Maurice

  7. Neil Lamka Says:

    First, thanks for your blog, it has been very helpful.

    I’ve got a problem and I’m having trouble finding someone to help or some place to look. I have a Word template that I’m trying to convert to use the new Office 2007 Ribbon UI. My problem is I have not figured out any way to get a custom, transparant bitmap a ribbon button.

    What I have is a C resource DLL that returns a iPictureDisp object that I pass back based on the get image event and that displays on the Ribbon. With the old commandbar buttons you specified both the image and a mask in order to create the transparant button. Since there does not seem to be any mask property or a get image mask type of event I don’t seem to be able to make my images transparant.

    So, can you either help educate me or point me to someone/place that might be able to help.

    Thanks
    Neil

  8. Patrick Schmid Says:

    Hi Neil,

    I don’t know the answer right now. I’ll try to find out and will post another comment.

    Patrick

  9. Patrick Schmid Says:

    Hi Neil,

    I haven’t tried this myself, but an Outlook add-in writer and book author (Ken Slovak) did. He thinks the picture object has to include the alpha transparency layer in it. He had a few pictures that were set up with the alpha layer when he was playing with it, and they appeared nicely transparent.
    I hope this helps,

    Patrick

  10. Nadya Bedyuk Says:

    Hi Patrick,

    I use the same way of creating add-in, but i have some difficulties with adding custom image to button. AFAIS you use imageMso. I tried to use image attribute and specify relationship Id, but i didn’t find any way to create valid relationship to .ico or .png file stored in the package.

    Do you have any thoughts on this point?

    Thanks, Nadya

  11. Neil Lamka Says:

    Hi Patrick,
    Thanks for the reply. Not a a very ignorant question. How do you include an alpha transparancy layer in an image? I’ve tried setting the “tranaparancy” for a bitmap but it did not seem to make any difference.

    Thanks again for the help.

    Neil

  12. Neil Lamka Says:

    Just a small update. I could not figure out how to do this wie a bitmap. What I did was to change all of my bitmaps to icons and made sure that the icon background was set to the screen (transparant) color and that seemed to work for me.

    I’m not sure how to do this with a bitmap but I tink I have a way to get the transparancy I needed by changing everything to icons.

    Neil

  13. Patrick Schmid Says:

    Nadya: I’ll have a post in the future on how to do this.

    Neil: From wikipedia:
    ————-
    Images are generally stored with a color depth of 2 (1-bit), 16 (4-bit), 256 (8-bit), 65,536 (16-bit), or 16.7 million (24-bit) colors (the bits represent the bits per pixel). 8-bit images can also be greyscale instead of indexed color. An alpha channel (for transparency) may be stored in a separate file, where it is similar to a greyscale image. A 32-bit version with integrated alpha channel has been introduced with Windows XP and is used within its logon and theme system; it has yet to gain wide support in image editing software.
    ————–
    mspaint only supports 24-bit bmps. I don’t know what other software might support 32 bits.

  14. Gary Winey Says:

    Hello,

    Is it possible to provide a download link? Our server is blocking all of the images and I need to learn how to do this.

    Thanks!