Word 2007: executing a custom add-in button programatically

Posted on 2009-03-31
Last Modified: 2012-06-27
I am in the process of migrating a body of Word automation from Word 2000 to 2007.

In Word 2007, the ExecuteMso method has been added, which allows programatically invoking the action associated with any built-in button.

I need to invoke the action associated with a button that a COM add-in has added to the Office Button menu, however; and I have not been able to locate a method for doing so.

Any guidance will be greatly appreciated.
Question by:dlhumphrey
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 24037116
You'll need to clarify what you mean by the button menu. When a com modifies the menu in Word 2007, this typically appears in the Add-Ins tab of the ribbon, in bar of its own with one or more buttons. Is that so?
In that case you do not need ExecuteMso.

Find the internal name of the bar by executing the following macro in a bland document. (Alt F11, paste it into New Macros and press F5 with the cursor within the procedure).

Then use the following macro to find the name of the control button in that bar (probably just one?). Once you have the internal name of the commandbar ,search it with the second macro for the macros that are triggered when the button is clicked ('onAction'). That's the name of the macro you want to invoke. (Note that most commandbars listed are not visible, and include legacy controls for compatibility with old macros)

Sub listBars()
' Paste this code into the NewMacros module of the Normal Template (Alt-F11 to open VBA editor)
' Press F5 to execute directly from here and paste list into new document.
' Then return to the active document to view the list and continue.
Dim cbar
Selection.TypeText "Full list of command bars" + vbCr
Selection.TypeText "CommandBar Caption: ... CommandBar name: ..." + vbCr
For Each cbar In CommandBars
    Selection.TypeText cbar.NameLocal + "= " + cbar.Name + vbCr
Selection.TypeText vbCr + "Now select the command bar to examine and double-click here: "
ActiveDocument.Fields.add Range:=Selection.Range, Type:=wdFieldEmpty, text:= _
        "MACROBUTTON listControls Find control", PreserveFormatting:=False
End Sub
Sub listControls()
Dim ctl, cbName As String
With Selection
On Error Resume Next ' some built in bars refuse to reeal information
.Collapse wdCollapseEnd
.TypeText vbCr
cbName = InputBox("Name of command bar to search?")
.TypeText "Searched commandbar '" + cbName + "'" + vbCr
.TypeText "Caption: ... onAction: ..." + vbCr
For Each ctl In CommandBars(cbName).Controls
    .TypeText ctl.Caption + "= " + ctl.OnAction + vbCr
End With
End Sub

Open in new window


Author Comment

ID: 24039737
Thank you for the explanation and code samples, DonQuiyote.  The situation I've got is a little different, however.  You are right that I need to offer a better description.
The COM add-in I'm dealing with is a DMS (Worksite Office 2007 Integration). It adds a Ribbon tab of its own to allow access to some DMS functionality, but also it adds buttons to the Office Button menu - i.e., officeMenu, the dropdown accessed via the Office Button at top left of window (as do some other COM add-ins, such as Adobe's PDFMaker).

The specific button I need to execute is named WordWorkSiteSave -- this I got by adding the button to the QAT and viewing the Word.qat file (XML). The COM add-in's namespace is oUTR02K.Connect.

In the Word Options | Customize dialog, if I hover the pointer above the button corresponding to the one on the officeMenu, Word indicates in parentheses, the way an mso name would be, CustomControl.
I need to get a reference in code to the CustomControl named WordWorkSiteSave , added to the officeMenu by the COM add-in oUTR02K.Connect so that I can execute it whenever a user creates a new document.

Accepted Solution

DonQuiyote earned 500 total points
ID: 24046176
All right you seem familiar with setting up a macro and finding the information that is available.
'If' the COM makes its interface available, then you should be able to add a reference from your project, and then see in Object Browser the list of public procedures. In the VB Editor use tools, references. Either look through the list, or directly browse to the COM add-in if you know to locate it. Let me know how you get on.

Author Comment

ID: 24055214
The WorkSite DMS COM add-in exposes a lot of automation -- in fact, all that the WordWorkSiteSave button does can be done from within VBA, call by call.  (In an earlier version of the DMS, that was in fact necessary.)
Unfortunately the vendor dev support started me looking down the unfruitful ExecuteMSO path; they indicated there might be no straightforward way to reference and execute their button but suggested it might be possible round-aboutly through that method, somehow or other.
Looking more deeply than previously through the exposed WorkSite objects, today I located a class added specifically for Word 2007 that includes a method that appears to execute the WordWorkSiteSave button .  
Still, the method requires passing a reference to the button -- but existence of the method implies to me that someone at the vendor knows how to do that.  Next step I see is to go back to their dev support to inquire pointedly about this class and method that they seem to have been unaware of previously.
Thanks for your time and input, DonQuiyote.  You get the points on this.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Outline Suppose you have some simple text based data in Excel that you would like to display as a PowerPoint presentation. Of course it would be possible to write some fairly complex VBA code that created a new slide for each line of the Excel data…
Lately there has been a variety of news related to U.S. employment.  Stories about worker productivity, automobile and airline unions, low employment and foreign laborers have frequented the news.  Each story has good and bad attributes we might arg…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question