CommandBar Problem

D

DanC

Here's our problem. We create a class which initializes a member-wide
reference to a new commandbar:

set m_objCommandBar = CommandBars.Add("MyToolBar")

If we set m_objCommandBar.Visible=True immediately after this line of code,
then "MyToolbar" becomes visible (as we would expect).

If, however, we open a new document, and then call a method on the above
class that sets m_objCommandBar.Visible = False, the m_objCommandBar.Visible
property does appear to be "False", but the command bar does not get hidden.
On the other hand, if we change to hide method to be the following:

CommandBars("MyToolbar").Visible=False

Then the toolbar does get hidden properly.

The question is: why doesn't m_objCommandBar.Visible=FALSE work as expected
(as opposed to CommandBars("MyToolbar").Visible=False)?

One interesting side note to this is that objptr(m_objCommandBar) does *not*
equal objptr(CommandBars("MyToolBar")), which makes it seem like they are not
pointing to the same object. Another interesting point is that this behavior
only occurs if we open an additional document.

The complete code for the class is shown below my signature.

Thanks for your help...

Dan

'===================================================
' Code for class to demonstrate CommandBar problem
'===================================================

Option Explicit

Private WithEvents m_appWord As Word.Application
Private m_objDoc As Word.Document
Private m_objCommandBar As Office.CommandBar
Private Declare Sub OutputDebugString Lib "kernel32" Alias
"OutputDebugStringA" (ByVal lpOutputString As String)


Private Sub Class_Initialize()
Dim objButton As Office.CommandBarButton
Set m_appWord = New Word.Application
Set m_objCommandBar = m_appWord.CommandBars.Add("MyToolbar")
Set objButton = m_objCommandBar.Controls.Add(msoControlButton)
With objButton
.Style = msoButtonCaption
.Caption = "Test"
End With
m_appWord.Visible = True
m_objCommandBar.Visible = True ' This works
Set m_objDoc = m_appWord.Documents.Add
m_objCommandBar.Visible = False ' This works
m_objCommandBar.Visible = True ' This works

' m_objCommandBar and m_appWord.CommandBars(" MyToolBar") appear to point to
different objects
Logit "objptr(m_objCommandBar) = " & Hex$(ObjPtr(m_objCommandBar)) & ".
objptr(m_appWord.CommandBars("" MyToolBar"")) = " &
Hex$(ObjPtr(m_appWord.CommandBars("MyToolBar")))

End Sub

Private Sub Class_Terminate()

On Error Resume Next
If Not m_appWord Is Nothing Then
m_objCommandBar.Delete
Set m_objCommandBar = Nothing
m_appWord.Quit False
Set m_appWord = Nothing
End If
End Sub

Private Sub m_appWord_DocumentChange()
On Error GoTo hErr

If m_appWord.ActiveDocument.Name <> m_objDoc.Name Then ' Problems begin
after second document is added
' m_objCommandBar.Visible = False ' The toolbar will still be visible
after this
' Logit "Set m_objCommandBar.Visible= False. m_objCommandBar.Visible = "
& m_objCommandBar.Visible & ". m_appWord.CommandBars("" MyToolBar"").Visible
= " & m_appWord.CommandBars("MyToolBar").Visible
m_appWord.CommandBars("MyToolBar").Visible = False ' The toolbar will be
hidden after this
Logit "Set m_appWord.CommandBars(""MyToolBar"").Visible = False.
m_objCommandBar.Visible = " & m_objCommandBar.Visible & ".
m_appWord.CommandBars("" MyToolBar"").Visible = " &
m_appWord.CommandBars("MyToolBar").Visible
Else
m_objCommandBar.Visible = True
Logit "Set m_objCommandBar.Visible= True. m_objCommandBar.Visible = " &
m_objCommandBar.Visible & ". m_appWord.CommandBars("" MyToolBar"").Visible =
" & m_appWord.CommandBars("MyToolBar").Visible
End If
Exit Sub
hErr:
Logit "Error " & Err.Number & ": " & Err.Description

End Sub

Private Sub Logit(sMsg As String)
Debug.Print sMsg
OutputDebugString sMsg
End Sub


'===================================================
' End of Code for class to demonstrate CommandBar problem
'===================================================
 
K

Klaus Linke

Hi Dan,

CommandBars can be stored in lots of places (Normal.dot, docs, attached
template, global templates).

Since you don't specify the CustomizationContext in your code, maybe you
have created two command bars with the same name, in different places?

To check for that possibility:

Dim myCB As CommandBar
For Each myCB In CommandBars
If myCB.Name = "myToolBar" Then
MsgBox myCB.Context
End If
Next myCB

Greetings,
Klaus
 

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