1
votes

I'm trying to create a add in for MS Word with VBA. It has a "AutoExec" procedure that creates a new item in the CommandBar("Text") collection (right click menu) and a "AutoExit" that deletes this created item. As an example, I tried the code below that create an item "How Many Pages?", which executes a macro displaying the number of pages in the active document.

This is the AutoExec Code:

Public Sub AutoExec() 
Dim objcommandbutton As CommandBarButton 
   Call MsgBox("AutoExec") 
   Set objcommandbutton = Application.CommandBars("Text").Controls.Add _ 
                                      (Type:=msoControlButton, Before:=1) 
   objcommandbutton.Caption = "How Many Pages?" 
   objcommandbutton.OnAction = "HowManyPages" 
End Sub 

This is the AutoExit Code:

Public Sub AutoExit() 
Dim objcommandbutton As CommandBarControl 
   Call MsgBox("AutoExit") 
   For Each objcommandbutton In Application.CommandBars("Text").Controls 
      If objcommandbutton.Caption = "How Many Pages?" Then 
         objcommandbutton.Delete 
      End If 
   Next objcommandbutton 
End Sub

This is the Main Macro Code:

Public Sub HowManyPages() 
   If Documents.Count > 0 Then 
      Call MsgBox(ActiveDocument.BuiltInDocumentProperties("Number of Pages")) 
   Else 
      Call MsgBox("No document is currently active.") 
   End If 
End Sub 

When exiting the document, the Button previously added in CommandBars("Text") collection is not deleted. I see this when I open a new blank Word document and the button remains in the right click menu.

I know that the routine is performed correctly because there is a MsgBox instruction to verify it. This only happen with the AutoExit subroutine of a add-in, that is, loaded as a add-in: running the code in a macro with vba module works fine.

Any help?

2
Please embed your code in the post itself. See minimal reproducible example.Mathieu Guindon
Ok, I edited the question to include the code.Diogo
The CommandBars API was deprecated with Office 2007 and the Ribbon. The article you're linking to references Word97-2003. Have you tried researching how to work with the Ribbon?Mathieu Guindon
The procedure works fine with the ribbon, the Command Bar is created in a Add-in tab on the ribbon. But I only used the article as an example, my intention was to create (and delete) an item in the "Text" command bar (right click with the mouse). I've edit the question to include all the code.Diogo
I have a similar problem: years ago, experimenting with the commandbar in VBA, I messed it up, and I was never able to reset it; now the "Add ons" tab is filled with dozens of buttons, all idnetical, all calling my same macro.... How do I cleanup it in WORD 2010?jumpjack

2 Answers

1
votes

When working with the CommandBars object model in Word it's necessary to always specify the Application.CustomizationContext.

Word can save keyboard layouts and CommandBar customizations in various places: the Normal.dotm template, the current template or the current document. The default when you create a CommandBar addition may not be the3 same as the default when trying to delete something.

Since this is an add-in, I assume you want the change for the entire Word environment (any open document). In that case, use the context NormalTemplate. Use this before any calls to CommandBar:

Application.CustomizationContext = NormalTemplate

Note: for saving a customization in the current document: = ActiveDocument; for saving in the template attached to the current document: = ActiveDocument.AttachedTemplate.

0
votes

I solved my problem with a workaround:

I was trying to "add" a template (.dotm) as a add-in (in the "Templates and Add-ins" window) to use my VBA project in a new document. That's why I was using the AutoExec() and AutoExit() procedures. But only now I figure out that just "attaching" the .dotm template to the active document (in the same "Templates and Add-ins" window, as show in the figure below) makes the functions Private Sub Document_Open() and Private Sub Document_Close() to run normally. Which solves my problem.

Even so, I think there is a certain "issue" with the AutoExit() procedure when trying to change the CommandBars itens. But that's ok for now.

enter image description here