How do I get a handle on a text frame from the text inside it?

  • Thread starter Wolfgang Hugemann
  • Start date
W

Wolfgang Hugemann

The question seems so easy, but I couldn't find the answer on the web.

The cursor is placed within a text frame and I would like to have a
handle to that frame to find out about its size. I have got as far as
to check whether the cursor is within an textframe at all:

If Selection.StoryType = wdTextFrameStory Then....

But this does not give me a clue how to get a handle on that text
frame. I thought that the parent property would be a good idea, but
this seems to point at the active document, not at the text frame as
the next parent in hierachy.

Any ideas how to solve this problem?
 
H

Helmut Weber

Hi Wolfgang,

like this:

Sub Macro1()
If Selection.StoryType = wdTextFrameStory Then
MsgBox Selection.ShapeRange.Height
MsgBox Selection.ShapeRange.Width
MsgBox Selection.ShapeRange.Top 'etc
End If
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
W

Wolfgang Hugemann

Helmut,

thank you very much for that hint. I knew about the ShapeRange
Collection, but I always considered it to contain all child objects of
a text range. I did not even try to adress the parent of the current
object by using the ShapeRange Collection!

I checked your code and it works perfectly: The ShapeRange collection
of that text range has only one member an that is the parent object
that contains the text.

One more question: Although you skipped the index ShapeRange(1), the
code works. VBA seems to pick the first object of the collection by
default. Is this a general behaviour or has it to be attributed to the
fact that this collection contains only one element?

Thank you again, because my research had consumed a lot of time, before
I asked over here!

Wolfgang Hugemann
 
H

Helmut Weber

Hi Wolfgang,
One more question: Although you skipped the index ShapeRange(1), the
code works. VBA seems to pick the first object of the collection by
default. Is this a general behaviour or has it to be attributed to the
fact that this collection contains only one element?

I don't know, seems to me that there can only be one
shaperange in the selection at all.

Sometimes it's good not to know too much. ;-)
I didn't spent a thought on a collection of shaperanges.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top