Word VBA to remove the text box while keeping the text

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Word VBA to remove the text box while keeping the text

Post by Goshute »

I was sent a hideously munged Word document that originated from an OCR scanned document. Every heading, paragraph, and even individual paragraph returns was in either a frame or a textbox. I was able to strip the frames with this:

Code: Select all

Sub RemoveFrames() 
  Dim intC As Integer 
  For intC = ActiveDocument.Frames.Count To 1 Step -1 
    ActiveDocument.Frames(intC).Delete 
  Next intC 
End Sub
And I discovered I can manually convert the text boxes to frames, then run the macro on those, but couldn't figure out how to get to textboxes in the Word object model to automate that process. Can someone provide some pointers?
Goshute
I float in liquid gardens

User avatar
HansV
Administrator
Posts: 78489
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word VBA to remove the text box while keeping the text

Post by HansV »

Text boxes are shapes. The following code will convert all text boxes in the active document to frames:

Code: Select all

Dim i As Integer
For i = ActiveDocument.Shapes.Count To 1 Step -1
  If ActiveDocument.Shapes(i).Type = msoTextBox Then
    ActiveDocument.Shapes(i).ConvertToFrame
  End If
Next i
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12606
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Word VBA to remove the text box while keeping the text

Post by StuartR »

Goshute wrote:...couldn't figure out how to get to textboxes in the Word object model to automate that process. Can someone provide some pointers?

Code: Select all

Sub AllTextBoxes()
Dim shp As Shape
    
    For Each shp In ActiveDocument.Shapes
        If blnShapeSupportsAttachedText(shp) Then
            ' do something to the shape here
            ' MsgBox shp.Name
            End If
    Next shp

End Sub

Function blnShapeSupportsAttachedText(shpIn As Shape) As Boolean
    blnShapeSupportsAttachedText = False
    On Error GoTo Done
    If shpIn.TextFrame.HasText Then blnShapeSupportsAttachedText = True
Done:
End Function
StuartR


User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: Word VBA to remove the text box while keeping the text

Post by Goshute »

HansV wrote:Text boxes are shapes.
If ActiveDocument.Shapes(i).Type = msoTextBox Then
Perfect, thank you. (I got to the understanding they were shapes but got stuck when I couldn't see how to identify just the textboxe shape.)
Goshute
I float in liquid gardens

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: Word VBA to remove the text box while keeping the text

Post by Goshute »

StuartR wrote:shpIn.TextFrame.HasText
Thanks, Stuart. Always more than one way to skin the cat. :smile:
Goshute
I float in liquid gardens

User avatar
StuartR
Administrator
Posts: 12606
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Word VBA to remove the text box while keeping the text

Post by StuartR »

Goshute wrote:
StuartR wrote:shpIn.TextFrame.HasText
Thanks, Stuart. Always more than one way to skin the cat. :smile:
The two things are somewhat different. Hans approach will get to all text frames, which is what you asked for, mine will get to all text in any kind of shape, for example if you add a rectangle with no borders and no fill and put text in it then this is NOT a text frame.
StuartR