Share via

visual basic code for deleting the FIRST shape in my word doc

Anonymous
2013-11-01T22:52:03+00:00

I have a text box at the beginning of my word docs that instructs the user to do a few things. When they are done, I would like a macro to delete the text box. I used the following, and it works great IF THERE IS ONLY ONE TEXT BOX IN MY DOCUMENT. Unfortunately (of course) sometimes there is more than one text box. How can I modify this to have it delete the FIRST text box only?

Sub SelectAllShapes()

ActiveDocument.Shapes.SelectAll

End Sub

Please keep in mind I am a vba beginner at best...

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2013-11-02T03:07:41+00:00

Somewhat more robust:

Sub JustTheFirstShape()

With ActiveDocument.Shapes

  If .Count <> 0 Then .Item(1).Delete

End With

End Sub

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2013-11-02T01:58:55+00:00

I don't do Word programming unless forced, but try this on for size...

Sub JustTheFirstShape()

ActiveDocument.Shapes(1).Delete

End Sub

The code should delete the the first shape added to the document.

(not necessarily the shape at the top of the document)

'---

Jim Cone

Portland, Oregon USA

https://goo.gl/IUQUN2 (Dropbox)

(free & commercial excel add-ins & workbooks)

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2013-11-04T05:53:07+00:00

    To delete the shape that appears first in the document, rather than the one that was first inserted, if it is not the one that appears first in the document, use:

    With ActiveDocument

        If .Shapes.Count <> 0 Then

            .range.ShapeRange.Item(1).Delete

        End If

    End With

    Was this answer helpful?

    0 comments No comments