PDA

View Full Version : Word is resetting .visible param for a command bar



HelenT
02-10-2016, 04:33 AM
Hi,

I'm running the following code from a template. The template is attached to a document which I am opening by double clicking the document in a file explorer window, hence AutoOpen is invoked. The code appears to run (msg box 2 pops up) but when I navigate to the add-ins tab, the button is not there and when I type ?CommandBars("ChessSrch").Visible into the immediate window, it is set to false.

If I then set the Visible param from the Immediate window (CommandBars("ChessSrch").Visible = true), the button is displayed on the add-ins tab and operates correctly.

Can anyone please tell me what is happening? Why is the setting of .visible in the AutoOpen sub being ignored/reset by Word?

thanks, Helen.

Dim commandBar As commandBar
Dim firstButton As CommandBarButton

Sub AutoOpen()
' force show of chess search toolbar
On Error GoTo ErrHandler

Set commandBar = CommandBars("ChessSrch")
' If "ChessSrch" CommandBar already exists, display it:
commandBar.Visible = True
MsgBox ("Chess AutoOpen ran 1")

ErrHandler:
Set commandBar = CommandBars.Add("ChessSrch", 1, False, True)

Set firstButton = commandBar.Controls.Add(1)
firstButton.Style = Office.MsoButtonStyle.msoButtonCaption
firstButton.Caption = "chess searches"
firstButton.Tag = "chess_search"
firstButton.OnAction = "ChessSearch_Main"
commandBar.Visible = True
MsgBox ("Chess AutoOpen ran 2")

End Sub

gmaxey
02-10-2016, 11:31 AM
Your macro appears to work fine here. You might want to insert Exit Sub before your ErrHandler: because as it is the ErrHandler code is running regardless if there is an error or not.

HelenT
02-17-2016, 03:36 AM
Hi Greg, thanks for your reply.

Does the button appear on your add-ins tab?

It still does not for me and I can't understand where/how .visible is getting reset to false :-(

Thanks, Helen.

gmaxey
02-17-2016, 05:03 AM
If you can send me the template I will try to figure out why. Send me website feedback and you can attach the template to my reply