changing colours of Visio shapes

W

will48

I'd like some help with macro code for toggling the colour of a Visio shape
when clicked. For example, fill colour alternates between red and green on
each click.

I can make one or more shapes turn red with a button and code:

Private Sub CommandButtonRed_Click()
Dim UndoScopeID1 As Long
UndoScopeID2 = Application.BeginUndoScope("Fill Properties")
Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC
(visSectionObject, visRowFill, visFillForegnd).FormulaU = "2"
Application.ActiveWindow.Page.Shapes.ItemFromID(8).CellsSRC
Application.EndUndoScope UndoScopeID1, True
End Sub

And a similar code for the turn green button.
However, this would require two buttons per shape, and I have many shapes.


Any help appreciated.
Will
 
J

John Goldsmith \(Visio MVP\)

Hello Will,

You just need to add a condition to check the state of your fill before you
change it. Have a go with the following:

Private Sub CommandButtonRed_Click()
Dim UndoScopeID1 As Long
UndoScopeID1 = Application.BeginUndoScope("Toggle fill")
Dim vCell As Cell
Dim shp As Shape
Set shp = ActiveWindow.Selection.PrimaryItem
If Not shp Is Nothing Then
Set vCell = shp.CellsSRC(visSectionObject, visRowFill,
visFillForegnd)
If vCell.ResultIU = "2" Then
vCell.FormulaU = "3"
Else
vCell.FormulaU = "2"
End If
End If
Application.EndUndoScope UndoScopeID1, True
End Sub

Hope that helps.

Best regards

John


John Goldsmith (Visio MVP)
www.visualSignals.typepad.co.uk
www.visualSignals.co.uk
 

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