Office Automation Copy/Paste Issue with Formatting

J

John

I've taken over a VB.NET application that opens RTF documents, performs
some editing, and performs a copy/paste of those RTF documents into a
Word Document. If I manually perform the same actions, formatting from
the source RTF documents is maintained when pasted into the destination
Word document. I see the proper margins.

However, when performing the same actions in code, the formatting from
the RTF documents is NOT maintained when pasted. I think the normal.dot
settings are used.

Following is a sample of the VB.NET code being used:
'Retrieve source RTF document contents.
Source.Activate()
Source.Selection.HomeKey(Word.WdUnits.wdStory)
Source.Selection.WholeStory()
Source.Selection.Copy()

'Paste into destination document.
Destination.Activate()
Destination.Selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting)

As you can see, I am using the "wdFormatOriginalFormatting" flag but it
doesn't seem to matter.

Curiously, if I set a breakpoint on the
Destination.Selection.PasteAndFormat() line, select the destination
document and perform a CTRL-V, I see the same incorrect formatting
issue.

If I then open a new instance of Word and perform a CTRL-V to copy the
same data from the Clipboard to the newly created document, the
formatting is correct.

Can someone out there please tell me what the heck I'm doing wrong?
-John
 
C

Cindy M -WordMVP-

Hi John,

You aren't really doing anything wrong. What you're running into is the fact that
automated Word won't necessarily behave like Word in the UI. Word does some things to
make life easier for the user that aren't part of the VBA interface.

If the formatting about which you're concerned is the formatting from page layout,
then what you're seeing is the result of the last paragraph mark (= last section
break) not being transferred during the copy/paste action. The commands pertaining to
formatting while pasting do NOT apply to section-level formatting.

My recommendation would be to try using Word.Selection.Paste; this is most likely to
give you the desired result, if anything can.

Otherwise, your options would be
- "read" the required formatting from the original document, then apply it to the
new document

- insert a section break at the end of the original document. when you copy/paste
this into the target document it should carry the section formatting with it.

- simply save the RTF document as a Word document?
I've taken over a VB.NET application that opens RTF documents, performs
some editing, and performs a copy/paste of those RTF documents into a
Word Document. If I manually perform the same actions, formatting from
the source RTF documents is maintained when pasted into the destination
Word document. I see the proper margins.

However, when performing the same actions in code, the formatting from
the RTF documents is NOT maintained when pasted. I think the normal.dot
settings are used.

Following is a sample of the VB.NET code being used:
'Retrieve source RTF document contents.
Source.Activate()
Source.Selection.HomeKey(Word.WdUnits.wdStory)
Source.Selection.WholeStory()
Source.Selection.Copy()

'Paste into destination document.
Destination.Activate()
Destination.Selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting)

As you can see, I am using the "wdFormatOriginalFormatting" flag but it
doesn't seem to matter.

Curiously, if I set a breakpoint on the
Destination.Selection.PasteAndFormat() line, select the destination
document and perform a CTRL-V, I see the same incorrect formatting
issue.

If I then open a new instance of Word and perform a CTRL-V to copy the
same data from the Clipboard to the newly created document, the
formatting is correct.

Can someone out there please tell me what the heck I'm doing wrong?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 

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