Word 2007: executing a custom add-in button programatically

Posted on 2009-03-31
Medium Priority
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
  • 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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Meetings to discuss business process can waste time, and often do .  The meeting's dialog can get confusing when participants have different professional perspectives and backgrounds.  A jointly-developed process picture helps wade through the confu…
A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

616 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