Delete Shapes from Word Macro Enabled Template using VBA


I have a Macro-Enabled Word Template that I have some shapes inserted.  When I go to save the document I would like to delete some of the shapes but not all of the shapes.

The following code deletes all of the shapes.  Is there a way to name the shapes individually so that they can be deleted in groups?

    Dim strToPath As String
    Dim strMsg As String
    Dim iResponse As Integer
    Dim i As Integer
    strToPath = Me.FormFields("strToPath").Result
    strMsg = strMsg & "If these steps are complete then select 'OK' to" & Chr(10)
    strMsg = strMsg & "permanently save the form to your file" & Chr(10)
    strMsg = strMsg & "or select 'Cancel' to return to the form." & Chr(10)
    iResponse = MsgBox(strMsg, vbOKCancel, "Final Instructions Prior to Saving Form")
        If iResponse = vbCancel Then
            Exit Sub
        End If

       'Delete ActiveX Command Button

          'Delete Shapes

         For i = ActiveDocument.Shapes.Count To 1 Step -1
         Next i

    ActiveDocument.SaveAs FileName:=strToPath, _
    With Application
        .ScreenUpdating = True
        .Quit SaveChanges:=wdDoNotSaveChanges
    End With
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You would have to use VBA.

Here are a couple of snippets that might point the way.
Sub ShapeNames()
    Dim sh As Shape
    For Each sh In ActiveDocument.Shapes
        MsgBox sh.Name
    Next sh
End Sub

Sub NameShape()
    Selection.ShapeRange(1).Name = "New Name"
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
isurgynAuthor Commented:
Hi Graham,

Thanks the code snippets look great.  In the time waiting for a solution I created a quick and dirty fix that didn't require naming the shapes but I will come back later and try your code as it will be a better solution for future projects.

What I did is just manipulate the shape count in a way that allowed me to delete unwanted shapes and keep the ones that I wanted.  A little trial and error to do so but only about 10 minutes of total time to move forward.

Thanks again.  Here is my quick fix.

    For i = ActiveDocument.Shapes.Count - 1 To 2 Step -1
    Next i
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.