Problem En/Disabling a Control in a CommandBar

E

EE

I am trying to get the "Save" button to become gray when there are no changes
in the file, e.g. immediately after Open, immediately after Save, ...
For this purpose I handle a handfull of events (e.g. PageAdded,
ShapeChanged, etc). The events handler checks the Enabled property of the
button and the Saved property of the document, and decides whether to enable
or disable the button.
For some reason, the button will sometimes refuse to be enabled. The only
way I manage to work around this is to Reset the "Standard" CommandBar.
Note that asimilar vba macro in Word works perfectly (including the SaveAll
button).

I am currently using Visio Pro 2003.

Can anyone help ?
 
J

JuneTheSecond

Would you please advise me whether you could change Enabled property in some
contexts when some menus not displayed?
 
E

EE

JuneTheSecond said:
Would you please advise me whether you could change Enabled property in some
contexts when some menus not displayed?

Sorry, I am not quite sure I understand your question.
I try to set the Enabled property of the button in event handlers. This
happens both when the document opens (to set the default state of the button
to Disabled, since when the document is opened it is yet unchanged), and
while editing, either on any shape change/addition/deletion, or when the
document is saved.
If I do not use :
Application.CommandBars("Standard").Reset
someplace at the beginning (I do this in Class_Initialize of
Visio.Application), then most times the button will not change its Enabled
state at all.
 
J

JuneTheSecond

I apporogize that i did not understand your question.
Really, the save button cannot be enabled.
Is it a bug? May be a bug.
I tied UIObject, it seems good. Use UIObject.
 
J

JuneTheSecond

for UIObject also seems to reset internaly all tool bars into default.
For example, insert any tool button in standard tool bar,
and the button cleared if the next lines runs.
Set vsoUIObject = Visio.Application.BuiltInToolbars(0)
ThisDocument.SetcustomToolbars vsoUIObject

So, my understanding is UIObject and Command Bar is the same,
if vsoCommandBar.Reset is added to the Commandbar to enable
the save button.
 
J

JuneTheSecond

Even in the case of CommandBar, enabling would works well when the macro
starts from the event proc, for ex. Document_ShapeAdded.
But in this case, the reset seems automatically run in somewhere.
Please try next macro.

Option Explicit

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
DisableSave
End Sub

Private Sub Document_ShapeAdded(ByVal Shape As IVShape)
EnableSave
End Sub

Public Sub DisAbleSaveButton()
Dim vsoCommandBars As Office.CommandBars
Dim vsoCommandBar As Office.CommandBar
Dim vsoCommandBarControl As Office.CommandBarControl
Dim vsoCommandBarButton As Office.CommandBarButton

On Error GoTo ERRMSG

Set vsoCommandBars = Application.CommandBars
Set vsoCommandBar = vsoCommandBars("Standard")
For Each vsoCommandBarControl In vsoCommandBar.Controls
Debug.Print vsoCommandBarControl.ID, vsoCommandBarControl.Caption
If vsoCommandBarControl.ID = 3 Then
vsoCommandBarControl.Enabled = False
Exit For
End If
Next
Exit Sub
ERRMSG:
MsgBox Err.Description
End Sub

Public Sub EnAbleSaveButton()
Dim vsoCommandBars As Office.CommandBars
Dim vsoCommandBar As Office.CommandBar
Dim vsoCommandBarControl As Office.CommandBarControl
Dim vsoCommandBarButton As Office.CommandBarButton

On Error GoTo ERRMSG

Set vsoCommandBars = Application.CommandBars
Set vsoCommandBar = vsoCommandBars("Standard")
For Each vsoCommandBarControl In vsoCommandBar.Controls
Debug.Print vsoCommandBarControl.ID, vsoCommandBarControl.Caption
If vsoCommandBarControl.ID = 3 Then
vsoCommandBarControl.Enabled = True
Exit For
End If
Next
ActiveWindow.Activate
Exit Sub
ERRMSG:
MsgBox Err.Description
End Sub
 
J

JuneTheSecond

I am sorry, Please forget previous post, I made big mistake, I called wrong
sub programs in the event procs.
 
J

JuneTheSecond

Now, I've come at the solution, it is very easy, just to put Enabled =False
before Enabled = True. This is my test macro. Save button disabled
when opened would be enabled when a shape is added.

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
DisAbleSaveButton
End Sub

Private Sub Document_ShapeAdded(ByVal Shape As IVShape)
EnAbleSaveButton
End Sub

Public Sub DisAbleSaveButton()
Dim vsoCommandBars As Office.CommandBars
Dim vsoCommandBar As Office.CommandBar
Dim vsoCommandBarControl As Office.CommandBarControl
Dim vsoCommandBarButton As Office.CommandBarButton

On Error GoTo ERRMSG

Set vsoCommandBars = Application.CommandBars
Set vsoCommandBar = vsoCommandBars("Standard")
For Each vsoCommandBarControl In vsoCommandBar.Controls
Debug.Print vsoCommandBarControl.ID, vsoCommandBarControl.Caption
If vsoCommandBarControl.ID = 3 Then
vsoCommandBarControl.Enabled = False
Exit For
End If
Next
Exit Sub
ERRMSG:
MsgBox Err.Description
End Sub

Public Sub EnAbleSaveButton()
Dim vsoCommandBars As Office.CommandBars
Dim vsoCommandBar As Office.CommandBar
Dim vsoCommandBarControl As Office.CommandBarControl
Dim vsoCommandBarButton As Office.CommandBarButton

On Error GoTo ERRMSG

Set vsoCommandBars = Application.CommandBars
Set vsoCommandBar = vsoCommandBars("Standard")
For Each vsoCommandBarControl In vsoCommandBar.Controls
Debug.Print vsoCommandBarControl.ID, vsoCommandBarControl.Caption
If vsoCommandBarControl.ID = 3 Then

vsoCommandBarControl.Enabled = False 'Dummy routine
vsoCommandBarControl.Enabled = True

Exit For
End If
Next

Exit Sub
ERRMSG:
MsgBox Err.Description
End Sub
 
E

EE

JuneTheSecond said:
Now, I've come at the solution, it is very easy, just to put Enabled =False
before Enabled = True. This is my test macro. Save button disabled
when opened would be enabled when a shape is added.
....snip...
vsoCommandBarControl.Enabled = False 'Dummy routine
vsoCommandBarControl.Enabled = True
....snip...

Now THIS has solved the problem. Thanx.
Just wanted to know :
1. Is this documented ?
2. If Y, where ?
3. If N
3.a. How did you ever come to thinking about this type of workaround, is
this a typical bug ?
3.b. Should this be reported to MS ?

Thanx again, you have been must helpfull.

EE
 
J

JuneTheSecond

I am sorry I don't know this type of matter is doccumented or not.
I think there are somebody already know what I did not know.
It is just one out of try and error, not logical.
 
E

EE

Well, thanx anyway.

If we are already solving such problems, I also tried changing the window
caption (wanted to add some "***" when the file has been modified, i.e. when
the Save button is enabled). Unfortunately, this does not work at all. If I
recall correctly, I tried something like ActiveWindow.Caption = "ABC", but
this has no effect.
Any more bright ideas ?
 

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