excel VBA更改形状的颜色

ldxq2e6h  于 2023-05-30  发布在  其他
关注(0)|答案(2)|浏览(334)

我在excel中有宏,它可以创建visio绘图。此宏在第一页上创建形状。之后,我想颜色这个形状,但我不能这样做。直到我的形状从基本形状变成“盒子”或“正方形”之类的东西。还可以。但是这个形状我不能做类似的动作。我发现,我的形状是一组形状。它包含12个其他形状,以及称为“RootShape”的东西。如果我试着旋转,或者移动形状-我可以。(当然,这是部分)但我不能给它上色。我试着像这样调用形状:

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

但我还是没有成功。
这是完整的代码

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

我尝试添加行/绘制其他行。我试着用颜色代码(ex 2代表红色)来绘画,我也试着从页面中调用形状作为对象(像这样,但当然不只是这样)

pagObj.Shapes.Item(1)

所以,我可以做的形状几乎任何我想要的,但不是着色。可能的问题是什么?(我的目标是shpObj.Shapes.Item(4))

djmepvbi

djmepvbi1#

这里的问题是形状有许多子形状,您的挑战是跟踪哪些子形状/s进行填充颜色。
要使用Drawing Explorer跟踪这类问题,macro recorderEvent Monitor工具会很有帮助。另外,打开各种子形状的ShapeSheet,看看在UI中应用手动更改时会发生什么。
需要注意的一个区别是,当您将填充颜色应用于组形状时,颜色将向下传播到所有子形状(除非它们被锁定),但在代码中,您一次只处理一个形状。
对于MainFrame形状(在“网络和外围设备”模具中),顶部组形状没有几何体,因此线条或填充格式将不会有任何影响。
如果在绘图资源管理器中检查形状,您将看到填充在第一和第三子形状中可见。
不幸的是,除了索引位置之外,它们缺少任何其他标识符。
因此,您的填充呼叫应寻址
shpObj.Shapes[1].CellsU("FillForegnd").FormulaU = "RGB(255,0,0)"
同样对于
shpObj.Shapes[3]...
Re RootShape-此属性仅获取对顶级组形状的引用。在您的场景中,shpObj.RootShape只返回一个对自身的引用。它实际上只对子形状有用。

8yparm6h

8yparm6h2#

我找到了我所错过的。我需要改变这个:

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

到这个

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

形状,其中“主机”组成的是具有复杂的着色方案(由于填充)。这就是问题所在。如果我改变“1”到“”我可以颜色的其他部分梯度

相关问题