MS Office Forum / Word / Programming / May 2008
Removing text boxes while preserving the text inside them
|
|
Thread rating:  |
John Doue - 08 May 2008 17:40 GMT 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
 Signature John Doue
Jean-Guy Marcil - 08 May 2008 18:11 GMT > 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
John Doue - 08 May 2008 18:40 GMT >> There are several macros around to remove text boxes from a document, >> but I cannot find a way to preserve the text inside them. [quoted text clipped - 20 lines] > 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
 Signature John Doue
Jean-Guy Marcil - 08 May 2008 20:13 GMT > 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?
John Doue - 09 May 2008 10:02 GMT >> 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 - 9 lines] > > 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
 Signature John Doue
Jean-Guy Marcil - 09 May 2008 13:17 GMT > >> 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 - 14 lines] > 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.
John Doue - 13 May 2008 09:35 GMT >>> 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
_________________________________________
 Signature John Doue
fumei - 13 May 2008 17:33 GMT 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.
>>>> 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 - 62 lines] > >_________________________________________ Jean-Guy Marcil - 13 May 2008 18:08 GMT > 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!
John Doue - 13 May 2008 19:00 GMT >> 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! 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
 Signature John Doue
Jean-Guy Marcil - 14 May 2008 13:39 GMT > >> 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. [quoted text clipped - 4 lines] > 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
???
John Doue - 14 May 2008 15:34 GMT >>>> 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. [quoted text clipped - 36 lines] > > ??? 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!
 Signature John Doue
John Doue - 13 May 2008 18:58 GMT > 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. [quoted text clipped - 65 lines] >> >> _________________________________________ 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
 Signature John Doue
|
|
|