I have dug around the help files but have been unable to find any
documentation on how to determine whether a paragraph or range contains a
text box. By "text box" I am referring not to a text box control on a user
form, but to the kind of text box that can be inserted from the main menu
with \Insert\Text Box. Also, how can the contents of the text box be
extracted -- I would like to remove the text box and leave the contents in
the document.

Thanks in advance,
Tom

Re: Text Boxes by Greg

Greg
Mon May 02 10:23:43 CDT 2005

Tom,

A textbox (as you describe) is a Shape object in the document drawaing
layer.

Here is a bit of crude code to count the textboxes in a range and
convert them to text.

Sub ScratchMacro()

Dim oRng As Word.Range
Dim oShp As Shape
Dim i As Integer

Set oRng = ActiveDocument.Range
'Count number of textboxes in range and convert to frame
For Each oShp In ActiveDocument.Shapes
If oShp.Type = msoTextBox Then
i = i + 1
oShp.ConvertToFrame
End If
Next
MsgBox "There are " & i & " textboxes in the range."
'Remove frame effects
For i = ActiveDocument.Frames.Count To 1 Step -1
With ActiveDocument.Frames(i)
.Borders.Enable = False
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
.Delete
End With
Next

End Sub

Notice that the textbox is first converted to a Frame which is in the
same layer as the main text. Then the frame effects (border and
shading) is removed and finally the frame itself is deleted. The
resulting text will appear in the main text at the point the text box
was anchored. You can see the anchor point by displaying non-printing
characters.


Re: Text Boxes by TT

TT
Mon May 02 10:43:23 CDT 2005

Thank you!
"Greg" <gmaxey@mvps.org> wrote in message
news:1115047423.633543.193690@z14g2000cwz.googlegroups.com...
> Tom,
>
> A textbox (as you describe) is a Shape object in the document drawaing
> layer.
>
> Here is a bit of crude code to count the textboxes in a range and
> convert them to text.
>
> Sub ScratchMacro()
>
> Dim oRng As Word.Range
> Dim oShp As Shape
> Dim i As Integer
>
> Set oRng = ActiveDocument.Range
> 'Count number of textboxes in range and convert to frame
> For Each oShp In ActiveDocument.Shapes
> If oShp.Type = msoTextBox Then
> i = i + 1
> oShp.ConvertToFrame
> End If
> Next
> MsgBox "There are " & i & " textboxes in the range."
> 'Remove frame effects
> For i = ActiveDocument.Frames.Count To 1 Step -1
> With ActiveDocument.Frames(i)
> .Borders.Enable = False
> With .Shading
> .Texture = wdTextureNone
> .ForegroundPatternColor = wdColorAutomatic
> .BackgroundPatternColor = wdColorAutomatic
> End With
> .Delete
> End With
> Next
>
> End Sub
>
> Notice that the textbox is first converted to a Frame which is in the
> same layer as the main text. Then the frame effects (border and
> shading) is removed and finally the frame itself is deleted. The
> resulting text will appear in the main text at the point the text box
> was anchored. You can see the anchor point by displaying non-printing
> characters.
>



Re: Text Boxes by Jay

Jay
Mon May 02 10:46:14 CDT 2005

TT wrote:
> I have dug around the help files but have been unable to find any
> documentation on how to determine whether a paragraph or range
> contains a text box. By "text box" I am referring not to a text box
> control on a user form, but to the kind of text box that can be
> inserted from the main menu with \Insert\Text Box. Also, how can the
> contents of the text box be extracted -- I would like to remove the
> text box and leave the contents in the document.
>
> Thanks in advance,
> Tom

Hi Tom,

A text box is a member of the Shapes collection, along with any AutoShapes
and floating pictures. You can figure out which Shapes are text boxes by
checking the value of the .Type property.

The contents of the text box are in the range given by its
.TextFrame.TextRange property. To display it, you could get that range's
.Text property:

Dim MyRange As Range
Dim oShp As Shape

Set MyRange = Selection.Range

On Error GoTo NoShapes
For Each oShp In MyRange.ShapeRange
If oShp.Type = msoTextBox Then
MsgBox oShp.TextFrame.TextRange.Text
End If
Next oShp
NoShapes:

When you extract the text from the box, where do you want to put it? The
problem is that the box doesn't have to be anywhere near its anchor, the
only requirement being that they're always on the same page. Dumping the
text at the anchor may make a confusing mess. If you can live with that,
though, the easiest way to extract the text is to (a) convert the text box
to a frame and (b) reset the frame's paragraph formatting, which removes the
frame and places the text at the anchor:

Dim MyRange As Range
Dim MyFrame As Frame
Dim oShp As Shape

Set MyRange = Selection.Range

On Error GoTo NoShapes
For Each oShp In MyRange.ShapeRange
If oShp.Type = msoTextBox Then
Set MyFrame = oShp.ConvertToFrame
MyFrame.Range.ParagraphFormat.Reset
End If
Next oShp
NoShapes:

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org



Re: Text Boxes by Helmut

Helmut
Mon May 02 10:56:36 CDT 2005

Hi Tom,

>I have dug around the help files but have been unable to find any
>documentation on how to determine whether a paragraph or range contains a
>text box.

They don't "contain" it at all. But the shape has an anchor property,
which tells you by way of the character, to which they are connected,
to which paragraph they belong.

So here we go:

Sub test570()
Dim oShp As Shape ' a shape
Dim lPrg As Long ' the position of the anchor
Dim rTmp As Range ' a temporary range
Dim nPrg As Long ' number of paragraph anchor is in
Dim lShp As Long ' counter for shapes
With ActiveDocument.Shapes
For lShp = .Count To 1 Step -1
If .Item(lShp).Type = msoTextBox Then
sTxt = .Item(lShp).TextFrame.TextRange.Text
lPrg = .Item(lShp).Anchor.Start
Set rTmp = ActiveDocument.Range(Start:=0, End:=lPrg)
nPrg = rTmp.Paragraphs.Count + 1
ActiveDocument.Paragraphs(nPrg).Range.Text = _
ActiveDocument.Paragraphs(nPrg).Range.Text & " " & sTxt
End If
Next
End With
End Sub

My example checks whether a frame is of the right type,
checks the number of the character in the doc it is connected
to (anchor), counts the number od paragraphs from doc's start to
the anchor, adds 1, as here it is about whole paragraphs,
reads the text from the shape and adds it to the text of
the paragraph in question, whereby the shape disappers
without any further action.

And all that doesn't mean that there would be anything
wrong with Greg's approach, as far as I see.

Have a nice day.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/









Re: Text Boxes by TT

TT
Mon May 02 12:04:06 CDT 2005

This approach should work well. The general nature of what I am doing is
cleaning up documents that are compiled from several independent files. In
some cases, one or more of the component files consist entirely of a single
text box. A typical compiled file might have the structure below:

Component 1:
normal paragraphs

Component 2:
1 big text box

Component 3:
normal paragraphs

"Helmut Weber" <elmkqznfwvccbf@mailinator.com> wrote in message
news:8pic7197qe5moijfe1vehoav19irqapa6r@4ax.com...
> Hi Tom,
>
>>I have dug around the help files but have been unable to find any
>>documentation on how to determine whether a paragraph or range contains a
>>text box.
>
> They don't "contain" it at all. But the shape has an anchor property,
> which tells you by way of the character, to which they are connected,
> to which paragraph they belong.
>
> So here we go:
>
> Sub test570()
> Dim oShp As Shape ' a shape
> Dim lPrg As Long ' the position of the anchor
> Dim rTmp As Range ' a temporary range
> Dim nPrg As Long ' number of paragraph anchor is in
> Dim lShp As Long ' counter for shapes
> With ActiveDocument.Shapes
> For lShp = .Count To 1 Step -1
> If .Item(lShp).Type = msoTextBox Then
> sTxt = .Item(lShp).TextFrame.TextRange.Text
> lPrg = .Item(lShp).Anchor.Start
> Set rTmp = ActiveDocument.Range(Start:=0, End:=lPrg)
> nPrg = rTmp.Paragraphs.Count + 1
> ActiveDocument.Paragraphs(nPrg).Range.Text = _
> ActiveDocument.Paragraphs(nPrg).Range.Text & " " & sTxt
> End If
> Next
> End With
> End Sub
>
> My example checks whether a frame is of the right type,
> checks the number of the character in the doc it is connected
> to (anchor), counts the number od paragraphs from doc's start to
> the anchor, adds 1, as here it is about whole paragraphs,
> reads the text from the shape and adds it to the text of
> the paragraph in question, whereby the shape disappers
> without any further action.
>
> And all that doesn't mean that there would be anything
> wrong with Greg's approach, as far as I see.
>
> Have a nice day.
>
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP
> "red.sys" & chr(64) & "t-online.de"
> Word XP, Win 98
> http://word.mvps.org/
>
>
>
>
>
>
>
>



Re: Text Boxes by Greg

Greg
Mon May 02 19:31:53 CDT 2005

Helmut,

Neat. I can see the advantages of this method. I didn't work out the bugs,
because I don't think I have a use for it. By bugs, I mean case where the
proposed new paragraph doesn't exist. For example if you have a document
with one paragraph and one or more textboxes anchored to it, errors are
generated. I am sure it wouldn't be too much trouble to resolve.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Helmut Weber wrote:
> Hi Tom,
>
>> I have dug around the help files but have been unable to find any
>> documentation on how to determine whether a paragraph or range
>> contains a text box.
>
> They don't "contain" it at all. But the shape has an anchor property,
> which tells you by way of the character, to which they are connected,
> to which paragraph they belong.
>
> So here we go:
>
> Sub test570()
> Dim oShp As Shape ' a shape
> Dim lPrg As Long ' the position of the anchor
> Dim rTmp As Range ' a temporary range
> Dim nPrg As Long ' number of paragraph anchor is in
> Dim lShp As Long ' counter for shapes
> With ActiveDocument.Shapes
> For lShp = .Count To 1 Step -1
> If .Item(lShp).Type = msoTextBox Then
> sTxt = .Item(lShp).TextFrame.TextRange.Text
> lPrg = .Item(lShp).Anchor.Start
> Set rTmp = ActiveDocument.Range(Start:=0, End:=lPrg)
> nPrg = rTmp.Paragraphs.Count + 1
> ActiveDocument.Paragraphs(nPrg).Range.Text = _
> ActiveDocument.Paragraphs(nPrg).Range.Text & " " & sTxt
> End If
> Next
> End With
> End Sub
>
> My example checks whether a frame is of the right type,
> checks the number of the character in the doc it is connected
> to (anchor), counts the number od paragraphs from doc's start to
> the anchor, adds 1, as here it is about whole paragraphs,
> reads the text from the shape and adds it to the text of
> the paragraph in question, whereby the shape disappers
> without any further action.
>
> And all that doesn't mean that there would be anything
> wrong with Greg's approach, as far as I see.
>
> Have a nice day.
>
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP
> "red.sys" & chr(64) & "t-online.de"
> Word XP, Win 98
> http://word.mvps.org/