Problem with FormattedText

R

RB Smissaert

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
 
C

Cindy M.

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 :)
 
R

RB Smissaert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top