【问题标题】:Is there a way to use a Shape's ID in PowerPoint VBA to then determine it's color?有没有办法在 PowerPoint VBA 中使用形状的 ID 来确定它的颜色?
【发布时间】:2020-10-31 11:48:50
【问题描述】:

我有一个宏,可以从大约 25 张单独的幻灯片中构建一个合并的 PowerPoint 演示文稿。幻灯片有一个红色、黄色或绿色的状态圈来显示当前状态。我想提取这些形状的颜色并放入一个 excel 文件中。我尝试使用形状索引号来引用我的代码中的形状,但特定形状的索引在每张幻灯片上都会发生变化;这是不一致的。形状 ID 似乎是一致的,但我无法弄清楚如何在 VBA 中使用形状 ID。这是我目前正在使用的:

With ActivePresentation.Slides(IForLoop).Shapes(8).Fill.ForeColor
data = ActivePresentation.Slides(IForLoop).Shapes(8).Fill.Forecolor

If Instr(1 , data, "255"), Then
     LArray (I, sStatus) = "Red"
ElseIf InStr(1, data, "65535") Then
     LArray (I, sStatus) = "Yellow"
ElseIf InStr(1, data, "5287936") Then
     LArray (I, sStatus) = "Green"
End If

问题在于每张幻灯片的 Shape 引用 Shape(8) 不一致,因此我没有得到正确的颜色名称来填充我的 Excel 文件。

谢谢。

【问题讨论】:

  • 按 id 或名称获取形状:stackoverflow.com/q/5527073/212869
  • 我之前确实看过那篇文章。我确实知道形状 ID,但不知道在 VB 代码中引用 ID 以查找填充颜色并传递给我的 if/then 语句的语法。

标签: vba powerpoint shapes


【解决方案1】:

未经测试:

'...
Dim shp As Shape

Set shp = getShapeByID(yourIdHere, ActivePresentation.Slides(IForLoop))

If Not shp Is Nothing then
   Select Case shp.Fill.ForeColor
       Case 255: LArray (I, sStatus) = "Red"
       Case 65535: LArray (I, sStatus) = "Yellow"
       Case 5287936: LArray (I, sStatus) = "Green"
   End Select
End If
'...


'Get a shape from its Id
Function getShapeByID(shapeID As Long, sl As Slide) As Shape
    Dim s As Shape
    For Each s In sl.Shapes
        If s.id = shapeID Then
            Set getShapeByID = s
            Exit Function
        End If
    Next
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多