Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

How to copy all textbox content of doc1 to doc2 - and back?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nomey - 15 Feb 2006 10:17 GMT
In a document, various text boxes with formatted text, even with tables
in them, exist.

I would like to cut all textbox content in all text boxes and copy it to
a second document for editing, and once edited, move the content back to
the original textboxes.

I have this macro (see below), but it copies (it should cut) and it
deletes all formatting and tables. Is there a better way to cut? How do
I get the content back to the original textboxes?

Shirley Nomey.

Sub CopyFromTextBoxes()
Dim I As Integer, Boite As Shape, ThisDoc As Document
Set ThisDoc = ActiveDocument
Documents.Add
For Each Boite In ThisDoc.Shapes
    If Boite.Type = msoGroup Then
       For I = 1 To Boite.GroupItems.Count
          With Boite.GroupItems(I).TextFrame
            If .HasText Then
                Selection.InsertAfter .TextRange
                Selection.InsertParagraphAfter
                Selection.Start = Selection.End
            End If
          End With
       Next
    Else
        With Boite.TextFrame
            If .HasText Then
                Selection.InsertAfter .TextRange
                Selection.InsertParagraphAfter
                Selection.Start = Selection.End
            End If
        End With
    End If
Next
End Sub
Jean-Guy Marcil - 15 Feb 2006 16:00 GMT
Nomey was telling us:
Nomey nous racontait que :

> In a document, various text boxes with formatted text, even with
> tables in them, exist.
[quoted text clipped - 6 lines]
> deletes all formatting and tables. Is there a better way to cut? How
> do I get the content back to the original textboxes?

Try this to transfer all text in textboxes to a new document.
Each textbox will span a section, if you have grouped textboxes, they will
be separated by a continuous section breaks, and then a page section break
to separate the group from the next textbox or next group.

Sub CopyFromTextBoxes()
Dim I As Integer
Dim Boite As Shape
Dim ThisDoc As Document
Dim TargetDoc As Document
Dim AddPageBreak As Boolean

Set ThisDoc = ActiveDocument
Set TargetDoc = Documents.Add
AddPageBreak = False

For Each Boite In ThisDoc.Shapes
    If Boite.Type = msoGroup Then
       For I = 1 To Boite.GroupItems.Count
          With Boite.GroupItems(I).TextFrame
            If .HasText Then
               AddPageBreak = True
                With TargetDoc.Range
                   .Collapse wdCollapseEnd
                   .FormattedText = Boite.GroupItems(I) _
                       .TextFrame.TextRange.FormattedText
                   .Collapse wdCollapseEnd
                   .InsertBreak wdSectionBreakContinuous
               End With
            End If
          End With
       Next
       If AddPageBreak Then
           With TargetDoc.Range
               .Collapse wdCollapseEnd
               .InsertBreak wdSectionBreakNextPage
           End With
       End If
    Else
        With Boite.TextFrame
            If .HasText Then
                With TargetDoc.Range
                   .Collapse wdCollapseEnd
                   .FormattedText = Boite _
                       .TextFrame.TextRange.FormattedText
                   .Collapse wdCollapseEnd
                   .InsertBreak wdSectionBreakNextPage
               End With
            End If
        End With
    End If
Next
End Sub

If you really want to delete the content of the original textboxes, add
   .TextRange.Delete
just before the closing "End If" of both
   If .HasText Then

Now, for thaw second part...
This is a bit more complicated.

Basically, scan the target document, each range between two "NextPage"
section breaks represents a text box, and if there are continuous section
breaks in that range, each sub-range (represented by the continuous section)
represents a textbox in a group. Ignore the last section. Use the same
syntax I used above to copy the section range back into the textbox ranges
(The Formatted text business).

One problem is the fact that some of the textbox groups may have textboxes
that do not have a textrange, how do you know which of the textbox in the
group has to be populated?
One answer is not to delete the textbox content in the original document (or
replace with generic text, such as "Content being edited", so the TextRange
HasText attribute remains true, so you just populate the textbox in the same
order that you target document is built.
Also, you have to make sure that the textboxes in the original document
remains as they are... No grouping changes or textbox additions/deletions.

I do not have code like this already written, so I cannot provide sample
code.

Try to write it, one step at the time, and write back in a new thread for
each specific question you may have.
Or, you maybe lucky and someone will be along shortly with code that was
written to do what you want...

But, I must say that seems like a lot of work for no real benefit.
Why do you need this procedure?
There may be an easier way to achieve your ends.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

Nomey - 16 Feb 2006 07:45 GMT
Hi Jean-Guy,

Thanks for your code. I'll try to write a reverse macro.

Shirley Nomey

 Marcil wrote:
> Try this to transfer all text in textboxes to a new document.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.