Doing a mailmerge from Excel with Word automation.
I am altering a document and then doing SaveAs.
This runs in a loop and after doing SaveAs I need the original document to
do the same again.
Rather than closing and reopening this document I prefer to keep the text +
formatting in a range
variable and putting that back. Only reason for this is that is quicker.

This is the simplified code I have:

Dim wd As Object
'need to add Word here as there also is a DAO Document!
Dim Doc As Word.Document
Dim rngAllText As Word.Range

Set wd = CreateObject("Word.Application")
Set Doc = wd.Documents.Open(strLetterDoc)

Set rngAllText = Doc.Content.FormattedText.Duplicate

'alter the document here
'do SaveAs here

Doc.Content.FormattedText = rngAllText

At this line line I get error 5937:
Cannot copy content between these 2 ranges.

Thanks for any advice in this.


RBS

Re: Problem with FormattedText by Cindy

Cindy
Thu Aug 10 04:02:46 CDT 2006

Hi RB,

> Doing a mailmerge from Excel with Word automation.
> I am altering a document and then doing SaveAs.
> This runs in a loop and after doing SaveAs I need the original document to
> do the same again.
> Rather than closing and reopening this document I prefer to keep the text +
> formatting in a range
> variable and putting that back. Only reason for this is that is quicker.
>
It's not going to work this way... The original range with formatting isn't
going to be saved "in memory" like this. It only creates a reference.

Simplest for you would be to copy to the Clipboard, then paste back in as
required.

More work, but more "user-friendly", would be to create a second, empty
document and use FormattedText to put the range into that. Then you can pull
it back out as required, again using FormattedText. At the end, you close this
"scratch file" without saving.

> This is the simplified code I have:
>
> Dim wd As Object
> 'need to add Word here as there also is a DAO Document!
> Dim Doc As Word.Document
> Dim rngAllText As Word.Range
>
> Set wd = CreateObject("Word.Application")
> Set Doc = wd.Documents.Open(strLetterDoc)
>
> Set rngAllText = Doc.Content.FormattedText.Duplicate
>
> 'alter the document here
> 'do SaveAs here
>
> Doc.Content.FormattedText = rngAllText
>
> At this line line I get error 5937:
> Cannot copy content between these 2 ranges.
>

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)


Re: Problem with FormattedText by RB

RB
Thu Aug 10 06:36:30 CDT 2006

OK, thanks for clarifying that.
I had in fact already changed this to the scratch file method as you
suggested in option 2.
I think this is better than the clipboard method and also better than
closing and re-opening
the original document.

RBS


"Cindy M." <C.Meister-C@hispeed.ch> wrote in message
news:VA.00000052.00ab2e44@speedy...
> Hi RB,
>
>> Doing a mailmerge from Excel with Word automation.
>> I am altering a document and then doing SaveAs.
>> This runs in a loop and after doing SaveAs I need the original document
>> to
>> do the same again.
>> Rather than closing and reopening this document I prefer to keep the text
>> +
>> formatting in a range
>> variable and putting that back. Only reason for this is that is quicker.
>>
> It's not going to work this way... The original range with formatting
> isn't
> going to be saved "in memory" like this. It only creates a reference.
>
> Simplest for you would be to copy to the Clipboard, then paste back in as
> required.
>
> More work, but more "user-friendly", would be to create a second, empty
> document and use FormattedText to put the range into that. Then you can
> pull
> it back out as required, again using FormattedText. At the end, you close
> this
> "scratch file" without saving.
>
>> This is the simplified code I have:
>>
>> Dim wd As Object
>> 'need to add Word here as there also is a DAO Document!
>> Dim Doc As Word.Document
>> Dim rngAllText As Word.Range
>>
>> Set wd = CreateObject("Word.Application")
>> Set Doc = wd.Documents.Open(strLetterDoc)
>>
>> Set rngAllText = Doc.Content.FormattedText.Duplicate
>>
>> 'alter the document here
>> 'do SaveAs here
>>
>> Doc.Content.FormattedText = rngAllText
>>
>> At this line line I get error 5937:
>> Cannot copy content between these 2 ranges.
>>
>
> Cindy Meister
> INTER-Solutions, Switzerland
> http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
> http://www.word.mvps.org
>
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
>