adding/deleting commandbar menu

D

Dennis S

I am a little confused about commandbars and their interaction with
normal.dot. I have a code template which is loaded during WORD application
startup via the startup folder. The AUTOEXEC code of the template loops
through all the controls of the "Menu Bar". If the custom commandbar exists,
it deletes the control. The custom control is then added to the "Menu Bar".

For Each ctrlCM in CommandBars("Meu Bar").Controls
if ctrl.Caption ="CM" Then
ctrlCM.Delete
endif
Next

Set cbrMenuCM = CommandBars("Menu
Bar").COntrols.Add(Type:=msocontrolPopup, temporary:=true)
cbrMenuCM.Caption = "CM"

Set btnCBarItem1 = cbrMenuCM.Controls.Add(Type:=msoControlButton,
temporary:=True)
with btnCBarItem1
.Caption = "Remove Extra Paragraphs"
.OnAction = "RemoveExtraParagraphs"
end with
 
P

Peter

This works well for me:

CustomizationContext = ActiveDocument.AttachedTemplate
' do my commandbar changes
ActiveDocument.AttachedTemplate.Saved = True.

That keeps Word from trying to save the template with my customized controls, and also let's me omit a big chunk of "delete the current controls" code.

hth,

-Peter
 
D

Dennis S

Thank you for your response.

This seems to work if WORD is manually opened first (i.e. WORD opens with a
blank document with normal template). However, it does not seem to work if
you double-click a word document. A VB error is encountered - "this command
is not available because no document is open". It appears WORD is trying to
apply the CustomizationContext prior to loading the document. Therefore,
there is no active document and hence the error.

Any ideas?
 
P

Peter

Hmm.... I have my code running in the AutoOpen macro, not the AutoExec, and am not encountering the error you describe. Try moving it there.

hth,

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top