Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1785
  • Last Modified:

Word 2007: executing a custom add-in button programatically

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.
  • 2
  • 2
1 Solution
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

dlhumphreyAuthor Commented:
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.
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.
dlhumphreyAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now