Select a SINGLE Shape using VBA

Hi All

I have some code (courtesy of Graham Skan*) it works a treat but when I try to adapt the code to another problem I get stuck.

Basically the code (modified below) rolls through the entire document and 1 by 1 selects each shape. What I want (need) is to select only a single shape - in-fact the shape that the user selects...

As soon as I remove the "For - Next" the code stops working... :-(

I've been trying things like:
for each sh in selection.shapes  (assuming only 1 shape is selected)

Thanks in advance,

kind regards, Steve


*Ref: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_20917936.html?sfQueryTermInfo=1+sh.anchor.insertbefor+sh.textframe.textrange.text
Sub RemoveBoxes() '(Optional HiddenYM As String = "")
    Dim sh As Shape
    Dim txt As String
    For Each sh In ActiveDocument.Shapes
        If sh.TextFrame.HasText Then
        txt = sh.TextFrame.TextRange.Text
        MsgBox (txt)
            'sh.Anchor.InsertBefore sh.TextFrame.TextRange.Text
            'sh.Delete
        End If
    Next sh
End Sub

Open in new window

LVL 15
DrTribosAsked:
Who is Participating?
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.

GrahamSkanRetiredCommented:
That was a while ago. Try it like this.
Sub TextFromShape() '(Optional HiddenYM As String = "")
    Dim sh As Shape
    Dim txt As String
    If Selection.ShapeRange.Count > 0 Then
        Set sh = Selection.ShapeRange(1)
        If sh.TextFrame.HasText Then
            txt = sh.TextFrame.TextRange.Text
            MsgBox "Text in selected shape is: " & txt
            'sh.Anchor.InsertBefore sh.TextFrame.TextRange.Text
            'sh.Delete
        Else
            MsgBox "Selected shape has to text facility"
        End If
    Else
        MsgBox "No shape selected"
    End If
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
DrTribosAuthor Commented:
Graham - thanks :-)

It works, I don't understand why so I'm going to have a play with the code, any further info would be greatly appreciated. Many thanks,

Steve
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.