【问题标题】:Change Shape color in a loop VBA PPT在循环VBA PPT中更改形状颜色
【发布时间】:2014-02-28 15:58:44
【问题描述】:

如果形状是某些连接器的 EndConnectedShape,我需要根据条件更改幻灯片中某些形状的颜色(连接器是根据 .txt 文件中的某些数据选择的,但数据输入部分工作正常)。

虽然它必须简单明了,但我尝试通过名称获取形状的部分仍然无法正常工作:

Sub test()
    Dim oFSO As FileSystemObject
    Set oFSO = New FileSystemObject

    Dim oFS As TextStream
    Dim i, j As Long

    Dim filePath, smth As String
    filePath = "C:\MyPath\MyFile.txt"

    If oFSO.FileExists(filePath) Then

        On Error GoTo Err

        Set oFS = oFSO.OpenTextFile(filePath)
        For i = 1 To 1
            smth = VecNames(j)  ' ADDED
            wholeLine1 = oFS.ReadLine
            VecNames = Split(wholeLine1, ",")

            wholeLine2 = oFS.ReadLine
            VecSIs = Split(wholeLine2, ",")


            For j = 1 To UBound(VecNames)
                With ActivePresentation.Slides(i)
                    For Each oSh In ActivePresentation.Slides(i).Shapes
                        If oSh.Connector And oSh.Name = smth Then
                            'Degub.Print oSh.Name

                            oSh.Line.ForeColor.RGB = RGB(255, 0, 0)
                            oSh.Line.Weight = VecSIs(j) * 5
                            strShNm = oSh.ConnectorFormat.EndConnectedShape.Name

                            ' NEXT LINE IS NOT WORKING :
                            mySh = ActivePresentation.Slides(i).Shapes(strShNm)
                            ' When tried to change the line above to the one below which is commented out, it DOESN'T work either:
                            ' mySh = Selection.ShapeRange(strShNm)
                            With mySh
                                mySh.Line.ForeColor.RGB = RGB(255, 0, 0)
                            End With

                        ElseIf oSh.Type = msoTextBox Then
                            If mySh.TextFrame.TextRange.Text = VecNames(j) Then
                                oSh.TextFrame.TextRange.Font.Color = RGB(255, 0, 0)
                            End If
                        End If
                    Next oSh
                End With
            Next j
        Next i
        oFS.Close
    Else
        MsgBox "The file path is invalid.", vbCritical, vbNullString
        Exit Sub
    End If
    Exit Sub

    Err:
        MsgBox "Error while reading the file.", vbCritical, vbNullString
        oFS.Close
        Exit Sub
End Sub

知道我做错了什么吗?谢谢!

【问题讨论】:

  • smth 的值是多少?你似乎从来没有设置它......它应该是VecNames(j)
  • 这可能会有所帮助。它描述了形状对象在办公室的使用:msdn.microsoft.com/en-us/library/office/…
  • @TimWilliams 非常感谢,我刚刚添加了该行。是的,它是一个 VecNames(j),当我直接使用它时它不起作用......
  • @user2761919 感谢您提供链接,我尝试将 mySh 的上述表达式更改为以下内容: mySh = Selection.ShapeRange(strShNm) ,但它仍然给我一个错误“需要对象”线......
  • Degub.Print oSh.Name 这段代码能编译吗?

标签: vba shapes powerpoint addressing


【解决方案1】:

在这段代码中:

strShNm = oSh.ConnectorFormat.EndConnectedShape.Name
mySh = ActivePresentation.Slides(i).Shapes(strShNm)

您从形状中获取名称,然后尝试从名称中获取形状...

更容易做到这一点(别忘了使用Set):

Set mySh =  oSh.ConnectorFormat.EndConnectedShape

【讨论】:

  • 亲爱的蒂姆·威廉姆斯,谢谢!!!!忘记设置了!!!那是问题!是的,我在你提出的方式之前做了(但没有设置)并且它不起作用,所以我决定将表达式分成两部分。谢谢!!!
  • 好吧,最后一句话,更好的解决方案: oSh.ConnectorFormat.EndConnectedShape.Line.ForeColor.RGB = RGB(255, 0, 0) 而不是所有以 strShNm 分配开头的行,直到 ElseIf .工作正常。感谢大家的反馈!
猜你喜欢
  • 2017-10-21
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 1970-01-01
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多