1

I need to change Colors of certain Shapes in a slide, based on the criteria if the shape is an EndConnectedShape of certain Connectors (the connectors are selected based on some data in a .txt file, but the data input part works fine).

Although it must be straightforward, the part where I try to get the Shape by its Name is still not working:

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

Any idea what am I doing wrong? Thanks!

T.B.
  • 15
  • 1
  • 5
  • 1
    What's the value of `smth` ? You don't seem to ever set it... Is it supposed to be `VecNames(j)` – Tim Williams Feb 28 '14 at 16:58
  • This might help. It describes the use of the shape object in office: http://msdn.microsoft.com/en-us/library/office/aa223088(v=office.11).aspx – MattB Feb 28 '14 at 16:59
  • @TimWilliams Thank you very much, I have just added the line. Yes, it was a VecNames(j), when I was using it directly it didn't work... – T.B. Feb 28 '14 at 17:08
  • @user2761919 Thanks for the link, I tried to change the above expression for mySh to the following: mySh = Selection.ShapeRange(strShNm) , but it still gives me an error "Object required" at this line.... – T.B. Feb 28 '14 at 17:10
  • `Degub.Print oSh.Name` does this code compile? – Tim Williams Feb 28 '14 at 17:12
  • @TimWilliams SORRY!!! It was commented out, but here when edditing I removed the '. I'll place it back, if you don't mind – T.B. Feb 28 '14 at 17:15
  • Interestingly, ActivePresentation.Slides(i).Shapes(strShNm).Select works fine! I usually add it above the line with mySh just to be sure if strShNm has the name I need – T.B. Feb 28 '14 at 17:30
  • I think the answer Tim provided is on the money. If you still have problems though you might want to try `Dim thisPresentation As Presentation` and then `Set thisPresentation = ActivePresentation` and then dive into the locals browser and make sure everything is what you think it is. You should be able to drill down to your shapes and find out how to appropriately reference them. - EDIT: Removed PowerPoint qualifier as it was pretty redundant. – MattB Feb 28 '14 at 17:38

1 Answers1

0

In this code:

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

You're getting the name from the shape, then trying to get the shape from the name...

Much easier to do this (and don't forget to use Set):

Set mySh =  oSh.ConnectorFormat.EndConnectedShape
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Dear Tim Williams, THANK YOU!!!! I forgot Set!!! That was the problem!!! Yes, I did before the way you propose (but without Set) and it didn't work, so I decided to separate the expression into two parts. Thank you!!! – T.B. Feb 28 '14 at 17:34
  • Well, the last remark, even better solution: oSh.ConnectorFormat.EndConnectedShape.Line.ForeColor.RGB = RGB(255, 0, 0) instead of all the lines starting with strShNm assignment and until ElseIf. Works fine. Thanks to everybody for your feedback! – T.B. Feb 28 '14 at 17:46