Color Each Shape of a Particular Layer

J

joegarber

So I have no problem changing the layer color, as shown by the line in
the code below with the comment "This line works...". But what I want
to do is change the foreground color of all the shapes in the layer.

It's supposed to change the color of all the shapes in the layer that
has the same name as the referenced shape's text. When changing the
layer color, it does that correctly; but when I try to change the
foreground color of each shape in the layer, it changes the color of
EVERY shape on the entire page.

I'm brand new to Visio and VBA... lil' help?

Public Sub ToggleLayerColor(ByRef shp As Visio.Shape)
Dim pagObj As Visio.Page
Dim layersObj As Visio.Layers
Dim layerObj As Visio.Layer
Dim shpsObj As Visio.Shapes
Dim shpObj As Visio.Shape
Dim i1 As Integer

Dim sLayer As String
sLayer = shp.Text

Set pagObj = ActiveDocument.Pages(1)
Set layersObj = pagObj.Layers
For Each layerObj In layersObj
If UCase(layerObj.Name) = UCase(sLayer) Then
'layerObj.CellsC(visLayerColor).FormulaU = "2" This line works
'Exit For
Set shpsObj = layerObj.Page.Shapes
For Each shpObj In shpsObj
shpObj.Cells("FillForegnd") = "2" ' Here is problem
Next
Exit For
End If
Next
End Sub
 
J

joegarber

The method name is a little misleading at the moment, since all it is
trying to do is change the color... but once it's able to do that I'll
make it toggle between colored or not, depending on the shape's current
state
 
J

JuneTheSecond

To me, it seems that a layer does not contains shapes, but that a shape
contains layer. And to me it seems that FillForegnd color cannot be shown
the layer color is active.
Sub test()
Dim shpObj As Visio.Shape
Dim I As Long, N As Long
Dim sLayer As String
sLayer = "MyLayer"
' Off the layer color
ActivePage.PageSheet.Cells("Layers.Color").FormulaU = "255"
'Change the fill color
For Each shpObj In ActivePage.Shapes
N = shpObj.LayerCount
If N > 0 Then
For I = 1 To N
If shpObj.Layer(I).Name = sLayer Then
shpObj.Cells("FillForegnd") = "2"
End If
Next
End If
Next
End Sub
 
J

joegarber

Excellent, thanks!
To me, it seems that a layer does not contains shapes, but that a shape
contains layer. And to me it seems that FillForegnd color cannot be shown
the layer color is active.
Sub test()
Dim shpObj As Visio.Shape
Dim I As Long, N As Long
Dim sLayer As String
sLayer = "MyLayer"
' Off the layer color
ActivePage.PageSheet.Cells("Layers.Color").FormulaU = "255"
'Change the fill color
For Each shpObj In ActivePage.Shapes
N = shpObj.LayerCount
If N > 0 Then
For I = 1 To N
If shpObj.Layer(I).Name = sLayer Then
shpObj.Cells("FillForegnd") = "2"
End If
Next
End If
Next
End Sub
 

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