Search code examples
excelvbavisio

VBA Change color of shape


I have macro in excel, which creates visio drawing. This macro creates shape on the first page. After that, i`d like to color this shape, but im unable to do this. Until my shape was something like "box" or "square" from basic shapes/etc. it was ok. But with this shape i can't do similar actions. I have found, that my shape is group of shapes. It contains 12 other shapes, and thing called "RootShape". If i try to rotate, or move shape - i can. (and it's parts of course) But i can't color it. I have tried to call shape like:

shpObj."do_the_coloring"
shpObj.RootShape."do_the_coloring"
shpObj.Shapes.Item(x)."do_the_coloring"

But i still had no sucess.

This is the full code

Option Explicit

Sub TEST()

   Dim AppVisio As visio.Application
   Dim docsObj As visio.Documents 
   Dim DocObj As visio.Document 
   Dim stnObj As visio.Document 
   Dim mastObj As visio.master 
   Dim pagsObj As visio.Pages 
   Dim pagObj As visio.Page 
   Dim shpObj As visio.shape 

   Set AppVisio = CreateObject("visio.application")      
   AppVisio.Visible = True
   
   
   Set docsObj = AppVisio.Documents
   Set DocObj = docsObj.Add("Detailed Network Diagram.vst")

   Set pagsObj = AppVisio.ActiveDocument.Pages
   Set pagObj = pagsObj.Item(1) 
   Set stnObj = AppVisio.Documents.Item("periph_m.vss") 'i use this stencil
   
   Set mastObj = stnObj.Masters("Mainframe") 'trying to color this shape
  
  Set shpObj = pagObj.Drop(mastObj, 2, 2) 'creating a figure
  'trying to paint it
   shpObj.CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "rgb(1,220,2)"
   shpObj.RootShape.CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "rgb(1,220,2)"
   shpObj.Shapes.Item(4).CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "rgb(1,220,2)"
   'other method
   shpObj.Cells("Fillforegnd").FormulaU = "RGB(255,0,0)"
   shpObj.RootShape.Cells("Fillforegnd").FormulaU = "RGB(255,0,0)"
   shpObj.Shape.Item(4).Cells("Fillforegnd").FormulaU = "RGB(255,0,0)"
   'other method
   shpObj.Fill.ForeColor.RGB = RGB (255, 0, 0)
   shpObj.RootShape.Fill.ForeColor.RGB = RGB (255, 0, 0)
   shpObj.Shapes.Item(4).Fill.ForeColor.RGB = RGB (255, 0, 0)
   'other similar methods
    ...
    End Sub

I tried to add rows/paint other rows. I tried to paint with color codes (ex 2 for red) I also tried to call shape as object from page (like this, but not just like this, of course)

pagObj.Shapes.Item(1)

So, i can do with the shape almost whatever i want, but not the coloring. What could be the possible problem? (my target is shpObj.Shapes.Item(4))


Solution

  • I have found, what i have missed. I needed change this:

    shpObj.Shapes.Item(4).CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "rgb(1,220,2)"
    

    to this

    shpObj.Shapes.Item(4).CellsSRC(visSectionFillGradientStops, 1, visGradientStopColor).FormulaU = "rgb(1,220,2)"
    

    shapes, which "mainframe" consists of are having complicated coloring scheme (because of filling). And this was the problem. If i change "1" to "0" I can color other part of gradient