Change fill color of shape through code

C

consulttech2004

I have been making my own master shapes. I note that I can change the
fill color of some master shapes through code using the following
logic:

Shape.FillStyle = "red"

Or whatever I have defined my style to be. It works pretty well for
our purposes. The problem is that some master shapes don't seem to
have a fill color, and some that I have drawn (all of them, in fact)
don't seem to want to change color through VBA no matter what I do.
Is there something I'm missing? Thanks in advance,

David
 
C

Chris Roth [Visio MVP]

Hi David,

FilyStyle applies an actual style to your shape. If you want to
manipulate individual characteristics, you need to set ShapeSheet cells
via code.

If you haven't seen the ShapeSheet, select a shape, then go to Window >
Show ShapeSheet. This spreadsheet thinghy defines the behavior of a shape.

John Goldsmith has a great "getting started" article that explains more:
http://visualsignals.typepad.co.uk/vislog/2007/10/just-for-starte.html

So, to programmatically change the fill color of a shape, you need to
set its FillForegnd ShapeSheet cell. Something like this:

shp.Cells("FillForegnd").Result = 2
shp.Cells("FillForegnd").Formula = "RGB(255,0,0)"

To edit masters properly, you need to start a sort of editing session:

Dim mst as Visio.Master
Dim mstCopy as Visio.Master
Dim shp as Visio.Shape

Set mst = ...get master somehow
Set mstCopy = mst.Open

Set shp = mstCopy.Shapes(1)
shp..Cells("FillForegnd").Formula = "RGB(255,0,0)"

mstCopy.Close
Set mstCopy = Nothing
Set shp = Nothing

If your shape is a group, then setting the group's fill might not do
anything, as the sub-shapes are what need colorin'. In this case, you
can recurse through shapes vis shp.Shapes.Item()

Probably not the easy answer you were looking for...!

--
Hope this helps,

Chris Roth
Visio MVP


Register for the 2008 Microsoft Office Visio Conference!
http://www.msvisioconference.com/

Visio Guy: Smart Graphics for Visual People
http://www.visguy.com
 
D

David Parker

...and not to mention that FillForegnd swaps places with FillBkgnd when you
chose a non-solid fill....
 

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