How can I change the background color of a shape using VBA?

Started by ziko, March 10, 2010, 06:22:10 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ziko

It seems very easy after couple of searches but it is not working for me.
I am looping through the shapes of a page and for certain shapes I would like to change the background color to green. here is my code:

Set lPage = mVisioApp.ActiveDocument.Pages(1)

    For Each lShape In lPage.Shapes
        If lShape.Data2 <> "" Then
            Call lShape.CreateSelection(visSelTypeAll)
            'I tried all of these, one at a time and nothing works
            lShape.CellsSRC(visSectionObject, visRowFill, visFillBkgnd) = 3
            lShape.CellsSRC(visSectionObject, visRowFill, visFillBkgnd).FormulaForceU = 3 '"RGB(0,255,0)"
            lShape.CellsSRC(visSectionObject, visRowFill, visFillBkgnd).FormulaU = 3 '"RGB(0,255,0)"
            lShape.Cells("FillBkgnd") = 3
            lShape.Cells("FillBkgnd").Result("") = 3
            lShape.BringToFront
        End If
    Next

I appreciate any help,
Ziko

wapperdude

Do you really want to change the background color?  That only shows when using fill patterns.  Also, the functionality of foreground and background reverse between solid and pattern fills.  Haven't looked at your code...

Visio 2019 Pro

ziko

Thanks for the reply.
yes I want to change the background color of the shape. So how can I modify those fill patterns from VBA?

wapperdude

I haven't tried all of your code, i.e., how you obtain your shape, but, the for the cellsSRC portion, either of these should work:

yourshape.CellsSRC(visSectionObject, visRowFill, visFillBkgnd).FormulaU = "3"

or

yourshape.CellsSRC(visSectionObject, visRowFill, visFillBkgnd).FormulaU = "RGB(0,255,0)"

HTH
Wapperdude
Visio 2019 Pro

wapperdude

I think you have to declare "lshape" as a shape...

Dim lshape As Visio.Shape


Wapperdude
Visio 2019 Pro

Visio Guy

Not sure why you have this line:

  Call lShape.CreateSelection(visSelTypeAll)

are you trying to walk sub-shapes inside of a group? If so, look for lShape.Shapes.

For Each shpSub in lShape.Shapes
   ...
Next shpSub
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

ziko


wapperdude

OK. 

For some reason, I don't see all of your code.  For example, I don't see the line VisioGuy addresses.

Very strange.
Visio 2019 Pro

ziko

It is my misunderstanding.
I used the FillForegnd instead of FillBkgnd and the colors shows.
Thanks all for your help.

Ziko