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

N

Nomey

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
 
J

Jean-Guy Marcil

Nomey was telling us:
Nomey nous racontait que :
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?

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.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
Top