Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Duplicate Range from Doc1 to Doc2 - Including Styles

Status
Not open for further replies.

mintjulep

Technical User
Aug 20, 2004
1,547
JP
I'm trying to figure out how to copy a range out of Doc1 into Doc2 while preserving Styles

This works:
Code:
'Get M9Content from G9R

With G9RDoc 'A Document Object
    BeginRange = .Bookmarks("Mark2").End
    EndRange = .Bookmarks("Mark3").Start
    Set G9R_M9Content = .Range(BeginRange, EndRange)
End With

'copy the G9R_M9Content to M9_M9Content

Set M9_M9Content = M9Doc.Bookmarks("M9Content").Range
G9R_M9Content.Copy
M9_M9Content.Paste

But I'd prefer to not have to resort to Copy and Paste

So I'm looking for the magic Range property that does this.

Code:
Range2.Everything = Range1.Everything

But I'm not having any luck. One would think, from reading
VBA Help said:
Range.Duplicate Property
Returns a read-only Range object that represents all the properties of the specified range.
Syntax

expression.Duplicate

expression Required. A variable that represents a Range object.

That
Range2 = Range1.Duplicate
should do the trick. But no.

Any suggestions?
 
.Duplicate merely allows you to specify a range to work on without upsetting whatever you've defined as the underlying range. It has nothing to do with copying & pasting. Try the 'FormattedText' method instead. For example:
ThisDocument.Paragraphs.Last.Range.FormattedText = ActiveDocument.Paragraphs.Last.Range.FormattedText

Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks for the hint.

Range1 has multiple paragraphs, each with a different style.

So it seems like I'd need to loop through them like thus:

Code:
Set M9_M9Content = M9Doc.Bookmarks("M9Content").Range
For Each P In G9R_M9Content.Paragraphs
    With M9_M9Content
        .Collapse (wdCollapseEnd)
        .InsertAfter (P.Range.FormattedText)
    End With
Next

Of course that doesn't work because .InsertAfter (Text as string) strips the formatting.

But I can't figure out how to put in the FormattedText.
 
[ponder]
Code:
For Each P In G9R_M9Content.Paragraphs
    With M9_M9Content
        .InsertParagraphAfter
        .Paragraphs.Last.Range.FormattedText = P.Range.FormattedText
    End With
Next

Almost works.

Except that each P.Range.FormattedText is insertted BEFORE the previously inserted text. Makes no sense to me.

.InsertParagraphAfter --> Inserts a new paragraph AFTER the last paragraph in the range. The range EXPANDS to include the new paragraph.

So the .LAST paragraph should now be the newly inserted paragraph.
 
Not quite sure why you are happy to use .InsertAfter, but not copy and paste (given that C&P does deal with your problem). Still, if we like Inserts, try InsertXML.

For example:

Dim XMLtext As String
XMLtext = Selection.XML
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.InsertXML XMLtext
 
Why aren't you simply inserting the whole of G9R_M9Content in one go?
M9_M9Content.Range.FormattedText = G9R_M9Content.Range.FormattedText
It really doesn't matter what's in the source, it will all be replicated in the destination. There's no need to loop through anything!!!

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top