Identifying parent TextBox for an InlineShape in Word

M

Mike Clayton

Hi,

I've got a VBA macro that performs actions on various InlineShape objects in
a Word document. I want to be able to determine whether or not a particular
InlineShape object is embedded inside a TextBox, but I can't see anything in
the object model that could help me do this.

I've tried <inlineshapeobject>.Parent, but this returns a reference to the
document, rather than the containing TextBox.

What I'm after is something like:

Public Sub ProcessInlineShape(value as Word.InlineShape)
Dim blnIsInsideTextBox as Boolean
' determine if the shape is inside a textbox
... mystery code goes here ...
' act based on the result
If blnIsInsideTextBox Then
' do something
Else
' do something else
End If
End Sub

Any help would be appreciated.

Regards,

Mike
 
H

Helmut Weber

Hi Mike,
I've got a VBA macro that performs actions on various InlineShape objects in
a Word document. I want to be able to determine whether or not a particular
InlineShape object is embedded inside a TextBox, but I can't see anything in
the object model that could help me do this.
Any help would be appreciated.

if so, how would you like to adress the inlineshapes?
Without looping through storyranges or shapes or both
it would be difficult to address an inlineshape outside
of the doc's mainstory at all, IMHO.
Unless, e.g. there are bookmarks holding the inlineshapes.

In case you manage somehow to address the inlineshapes, then,
it's range would reveal whether it is in a textframe,
whereby selection would have to be substituted by
the inlineshape's range.

But when looping through shapes you know whether
there are textframes anyway and
whether there are inlineshapes in them.

Sub Macro5()
Dim oShp As Shape
Dim oInl As InlineShape
For Each oShp In ActiveDocument.Shapes ' mainstory only
If oShp.Type = msoTextBox Then
For Each oInl In oShp.TextFrame.TextRange.InlineShapes
' ????
Next
End If
Next
End Sub


One more example, for a selected inlineshape:

If Selection.StoryType = wdTextFrameStory Then
MsgBox "in TextBox"
End If

There is a good deal of confusion in the terminology
and, if I may say so, in Word's design at all.

If one would be ten years younger and got
the experience one got today, one would do
some things differently.

Applies to Word-developers, too.

;-)

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Mike Clayton

Hi Helmut,

The Selection.StoryMode property helped a lot. I have to do a bit of messing
about with the Selection to be able to test the StoryType, but the following
code works well enough for me now.

Thanks for your help - I was getting ready to give up othewise.

Mike


' assign this sub to a commandbarbutton
Public Sub ProcessCurrentSelection()
Dim objInlineShape As Word.InlineShape
' check the user has an inline shape selected
If Not (Word.Application.Selection.Type = wdSelectionInlineShape) Then
MsgBox "You must have an inline shape selected."
Exit Sub
End If
' get a reference to the shape to process
Set objInlineShape = Word.Application.Selection.InlineShapes(1)
' process the inline shape
ProcessInlineShape objInlineShape
End Sub

Public Sub ProcessInlineShape(value As Word.InlineShape)
Dim objOldRange As Word.Range
Dim blnIsInsideTextBox As Boolean
' get a reference to the current selection in case the inlineshape isn't
selected in the window
Set objOldRange = Word.Application.Selection.Range
' select the inline shape so we can test the storytype
value.Select
' check if the inlineshape is inside a frame.
blnIsInsideTextBox = (Word.Application.Selection.StoryType =
wdTextFrameStory)
' restore the old selection so the user doesn't notice anything
different.
' (not tested if multiple windows are open from the same document - may
give odd results).
objOldRange.Select
' act based on the result
If blnIsInsideTextBox Then
' do something
value.Width = 200
Else
' do something else
value.Width = 100
End If
End Sub
 

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