Diogo
Diogo

Reputation: 733

CommandBars("Text").Controls not deleted when exiting the document - VBA word add-in

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?

Upvotes: 1

Views: 698

Answers (2)

Diogo
Diogo

Reputation: 733

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

Upvotes: 0

Cindy Meister
Cindy Meister

Reputation: 25693

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.

Upvotes: 1

Related Questions