none
Deleting shapes RRS feed

  • Question

  • I have the following macro that searches my document for revision bars. It then asks if you want to delete it. IF I say NO it goes to the next revision bar. If I say YES, it deletes that revision bar but skips the next revision bar and goes to the one after that and asks if you want to delete it. How do I get it to stop skipping over the next revision bar in my document?

     

    Sub FindRevisionBar()

    Dim elm As Shape

    Selection.HomeKey Unit:=wdStory, Extend:=wdMove

    For Each elm In ActiveDocument.Shapes

        If InStr(1, elm.Name, "vline", vbTextCompare) Then

        elm.Select

        gotoRange rng:=Selection.Range

        elm.Select

            Msg = "Do you want to DELETE this Revision Bar"

                Style = vbYesNo + vbCritical + vbDefaultButton2

                Title = "Revision Bar"

                Response = MsgBox(Msg, Style, Title)

                If Response = vbYes Then

                Selection.ShapeRange.Delete

            Else

                MyString = "No"

            End If

        End If

    Next elm

    Selection.HomeKey

    End Sub

    Thursday, October 13, 2011 3:57 PM

Answers

All replies

  • Try looping backwards:

    Sub FindRevisionBar()
        Dim i As Long
        Dim elm As Shape
        Dim Msg As String
        Dim Style As Long
        Dim Title As String
        Dim Response As Long
        For i = ActiveDocument.Shapes.Count To 1 Step -1
            Set elm = ActiveDocument.Shapes(i)
            If InStr(1, elm.Name, "vline", vbTextCompare) Then
                elm.Select
                gotoRange rng:=Selection.Range
                elm.Select
                Msg = "Do you want to DELETE this Revision Bar"
                Style = vbYesNo + vbCritical + vbDefaultButton2
                Title = "Revision Bar"
                Response = MsgBox(Msg, Style, Title)
                If Response = vbYes Then
                    Selection.ShapeRange.Delete
                Else
                    ' MyString = "No"
                End If
             End If
         Next i
    End Sub
    


    Regards, Hans Vogelaar
    Thursday, October 13, 2011 4:04 PM
  • That worked, thanks.

    Larry

    • Marked as answer by fuzzhead58 Thursday, October 13, 2011 4:17 PM
    Thursday, October 13, 2011 4:16 PM