Removing text boxes while preserving the text inside them

J

John Doue

There are several macros around to remove text boxes from a document,
but I cannot find a way to preserve the text inside them.

Can anybody point me to one which actually works?

Thanks a lot
 
J

Jean-Guy Marcil

John Doue said:
There are several macros around to remove text boxes from a document,
but I cannot find a way to preserve the text inside them.

Can anybody point me to one which actually works?

Dim rngPara As Range
Dim i As Long

With ActiveDocument
If .Shapes.Count > 0 Then
For i = .Shapes.Count To 1 Step -1
If .Shapes(i).Type = msoTextBox Or _
.Shapes(i).Type = msoAutoShape Then
If .Shapes(i).TextFrame.HasText Then
Set rngPara = .Shapes(i).Anchor
rngPara.Collapse wdCollapseStart
rngPara.FormattedText = .Shapes(i) _
.TextFrame.TextRange.FormattedText
.Shapes(i).Delete
End If
End If
Next
End If
End With
 
J

John Doue

Jean-Guy Marcil said:
Dim rngPara As Range
Dim i As Long

With ActiveDocument
If .Shapes.Count > 0 Then
For i = .Shapes.Count To 1 Step -1
If .Shapes(i).Type = msoTextBox Or _
.Shapes(i).Type = msoAutoShape Then
If .Shapes(i).TextFrame.HasText Then
Set rngPara = .Shapes(i).Anchor
rngPara.Collapse wdCollapseStart
rngPara.FormattedText = .Shapes(i) _
.TextFrame.TextRange.FormattedText
.Shapes(i).Delete
End If
End If
Next
End If
End With
Thanks Guy, but this is one I had tested which appears to do nothing: no
error messages, nothing (word 03). Is there some way you could
experiment a little with it?

Thanks
 
J

Jean-Guy Marcil

:

Thanks Guy, but this is one I had tested which appears to do nothing: no
error messages, nothing (word 03). Is there some way you could
experiment a little with it?

You mean you tried my code and it did nothing?
You actually tried it, right?

I just tested it with Word 2003 and all textboxes were removed while
preserving their content (text and format) at the anchor point.

What type of content are you testing this on?



If that is the case, are your textbox floating or inline with the text?
 
J

John Doue

Jean-Guy Marcil said:
:



You mean you tried my code and it did nothing?
You actually tried it, right?

I just tested it with Word 2003 and all textboxes were removed while
preserving their content (text and format) at the anchor point.

What type of content are you testing this on?



If that is the case, are your textbox floating or inline with the text?

Jean-Guy,

Yes, I of course did test your macro, it would have been very stupid and
rude of me not to do it!

The best way would be for me to send you an abstract of the document
since I am not sure I can precisely answer your questions. But since I
am not at liberty to publish this document, I need to send it to you
privately. Is this possible?

Best regards
 
J

Jean-Guy Marcil

John Doue said:
Jean-Guy,

Yes, I of course did test your macro, it would have been very stupid and
rude of me not to do it!

Just checking...Your previous message could have been interpreted to mean
that you had already seen a similar macro on the Internet, so you did not
need to test mine...
Also, I have seen strange behaviour from posters... so I now never assume
anything!
The best way would be for me to send you an abstract of the document
since I am not sure I can precisely answer your questions. But since I
am not at liberty to publish this document, I need to send it to you
privately. Is this possible?

You can send me a few pages that I can test. No guarantee though!
Are you in a hurry over this?

Unscramble this:

"MyFirstName(no hyphen)"_m AT hotmail period com
or
"jeanguy" "underscore" "the letter before N" AT hotmail period com

Cheers.
 
J

John Doue

John said:
Jean-Guy,

Yes, I of course did test your macro, it would have been very stupid and
rude of me not to do it!

The best way would be for me to send you an abstract of the document
since I am not sure I can precisely answer your questions. But since I
am not at liberty to publish this document, I need to send it to you
privately. Is this possible?

Best regards

As a follow up to this thread, Jean-Guy was kind enough to let me email
him an abstract of the problem document. It appears, the problem was,
textboxes in it were grouped, which prevented the macro from working.

Jean-Guy emailed me a macro which does the trick of ungrouping these
grouped boxes, saving the text and removing the boxes. This macro will
of course also work if the text boxes are not grouped.

The macro I am posting hereunder incorporates limited changes I made to
the one Jean-Guy emailed me, so if you find any problem with it, I am to
blame and not Jean-Guy.

Regards
__________________________________________
Option Explicit


Sub test()

Dim rngText As Range
Dim shpText As Shape
Dim i As Long
Dim j As Long

'To speed things up a bit, especially if you lots of textboxes.
Application.ScreenUpdating = False

With ActiveDocument
For i = .Shapes.Count To 1 Step -1
If .Shapes(i).Type = msoGroup Then
j = .Shapes(i).GroupItems.Count
.Shapes(i).Ungroup
'As we ungroup, the number of shapes increases, we have to
adjust
'Also, there might be groups within groups...
i = i + (j - 1)
End If
Next
For Each shpText In .Shapes
With shpText
If .Type = msoTextBox Then
If .TextFrame.HasText Then
Set rngText = .Anchor.Paragraphs(1).Range
With rngText
.Collapse Direction:=wdCollapseEnd
.FormattedText = shpText.TextFrame _
.TextRange.FormattedText
End With
shpText.Delete
End If
End If
End With
Next
End With

Application.ScreenRefresh
Application.ScreenUpdating = True

End Sub

_________________________________________
 
F

fumei via OfficeKB.com

John, thank you for doing a follow up. Not only with actual working code,
but also an explanation for what was the problem. Very good. Appreciated.

Kudos to Jean-Guy as well of course.


John said:
[quoted text clipped - 21 lines]
Best regards

As a follow up to this thread, Jean-Guy was kind enough to let me email
him an abstract of the problem document. It appears, the problem was,
textboxes in it were grouped, which prevented the macro from working.

Jean-Guy emailed me a macro which does the trick of ungrouping these
grouped boxes, saving the text and removing the boxes. This macro will
of course also work if the text boxes are not grouped.

The macro I am posting hereunder incorporates limited changes I made to
the one Jean-Guy emailed me, so if you find any problem with it, I am to
blame and not Jean-Guy.

Regards
__________________________________________
Option Explicit

Sub test()

Dim rngText As Range
Dim shpText As Shape
Dim i As Long
Dim j As Long

'To speed things up a bit, especially if you lots of textboxes.
Application.ScreenUpdating = False

With ActiveDocument
For i = .Shapes.Count To 1 Step -1
If .Shapes(i).Type = msoGroup Then
j = .Shapes(i).GroupItems.Count
.Shapes(i).Ungroup
'As we ungroup, the number of shapes increases, we have to
adjust
'Also, there might be groups within groups...
i = i + (j - 1)
End If
Next
For Each shpText In .Shapes
With shpText
If .Type = msoTextBox Then
If .TextFrame.HasText Then
Set rngText = .Anchor.Paragraphs(1).Range
With rngText
.Collapse Direction:=wdCollapseEnd
.FormattedText = shpText.TextFrame _
.TextRange.FormattedText
End With
shpText.Delete
End If
End If
End With
Next
End With

Application.ScreenRefresh
Application.ScreenUpdating = True

End Sub

_________________________________________
 
J

Jean-Guy Marcil

fumei via OfficeKB.com said:
John, thank you for doing a follow up. Not only with actual working code,
but also an explanation for what was the problem. Very good. Appreciated.

That was the price he had to pay...
Thanks for paying John!
 
J

John Doue

fumei said:
John, thank you for doing a follow up. Not only with actual working code,
but also an explanation for what was the problem. Very good. Appreciated.

Kudos to Jean-Guy as well of course.


John said:
Thanks Guy, but this is one I had tested which appears to do nothing:
no error messages, nothing (word 03). Is there some way you could
[quoted text clipped - 21 lines]
Best regards
As a follow up to this thread, Jean-Guy was kind enough to let me email
him an abstract of the problem document. It appears, the problem was,
textboxes in it were grouped, which prevented the macro from working.

Jean-Guy emailed me a macro which does the trick of ungrouping these
grouped boxes, saving the text and removing the boxes. This macro will
of course also work if the text boxes are not grouped.

The macro I am posting hereunder incorporates limited changes I made to
the one Jean-Guy emailed me, so if you find any problem with it, I am to
blame and not Jean-Guy.

Regards
__________________________________________
Option Explicit

Sub test()

Dim rngText As Range
Dim shpText As Shape
Dim i As Long
Dim j As Long

'To speed things up a bit, especially if you lots of textboxes.
Application.ScreenUpdating = False

With ActiveDocument
For i = .Shapes.Count To 1 Step -1
If .Shapes(i).Type = msoGroup Then
j = .Shapes(i).GroupItems.Count
.Shapes(i).Ungroup
'As we ungroup, the number of shapes increases, we have to
adjust
'Also, there might be groups within groups...
i = i + (j - 1)
End If
Next
For Each shpText In .Shapes
With shpText
If .Type = msoTextBox Then
If .TextFrame.HasText Then
Set rngText = .Anchor.Paragraphs(1).Range
With rngText
.Collapse Direction:=wdCollapseEnd
.FormattedText = shpText.TextFrame _
.TextRange.FormattedText
End With
shpText.Delete
End If
End If
End With
Next
End With

Application.ScreenRefresh
Application.ScreenUpdating = True

End Sub

_________________________________________

Glad this was of interest to you, I was under the impression this issue
was of limited interest. Did you actually test the code?

Regards
 
J

John Doue

Jean-Guy Marcil said:
That was the price he had to pay...
Thanks for paying John!
You are welcome, I always try to keep my side of bargains. Did struggle
a few hours to make the code work until I understood - more or less -
what the problem was.

Regards
 
J

Jean-Guy Marcil

John Doue said:
You are welcome, I always try to keep my side of bargains. Did struggle
a few hours to make the code work until I understood - more or less -
what the problem was.

For those interested, and also, for those who might be able to exlpain what
happened, because I can't...

The problem was that I sent him some perfectly good code by email (I used
OE, which manages my Hotmail account). When John got it, for some reason, 90%
of the periods "." at the start of lines within With blocks had been
stripped.

So, for example, instead of:

With rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "1", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
End With

He got:

With rngText
InsertParagraphAfter
Collapse wdCollapseEnd
Text = strTextFiller
.Bookmarks.Add strBookName & "1", rngText
InsertParagraphAfter
Collapse wdCollapseEnd
End With

???
 
J

John Doue

Jean-Guy Marcil said:
For those interested, and also, for those who might be able to exlpain what
happened, because I can't...

The problem was that I sent him some perfectly good code by email (I used
OE, which manages my Hotmail account). When John got it, for some reason, 90%
of the periods "." at the start of lines within With blocks had been
stripped.

So, for example, instead of:

With rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "1", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
End With

He got:

With rngText
InsertParagraphAfter
Collapse wdCollapseEnd
Text = strTextFiller
.Bookmarks.Add strBookName & "1", rngText
InsertParagraphAfter
Collapse wdCollapseEnd
End With

???
Jean-Guy,

As I explained privately to you, the fact is you sent me the code in an
html post. When your post is viewed as original html, some periods do
not show (I found this to be true both with Seamonkey and Thunderbird).

When viewed as text, the periods reappear. So, the reason has to do with
html, and my suggestion is, you post only in text format, which is the
only way I see to make sure such problems do not occur.

But don't ask me why this is happening, I am not an Html expert!
 

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