PDA

View Full Version : Paste Command Failing



ill_comms
08-19-2008, 10:31 PM
Hi, I have a Word Macro that does the following:

1. Opens Word document with macro in it "DOC1".
2. Creates a new document "DOC2".
3. Opens another document "DOC3" that is acting like a template of sorts.
4. DOC2 is selected and copies the entire document to the clipboard. Then pastes it into DOC3.



Documents(templateDocumentName).Select
Selection.WholeStory
Selection.Copy
Documents(newDocumentName).Activate
Selection.PasteAndFormat (wdPasteDefault)


This works perfectly in XP however falls over when run in Vista, both machines are using Office 2003.

The error is occuring on: "Selection.PasteAndFormat (wdPasteDefault)" and only producing a "Command Failed" error.

What could be causing this and does anyone have a fix?

Regards Hayden

macropod
08-20-2008, 07:19 AM
Hi Hayden,

I suspect the problem is that the code is running too fast for the document activation and selection processes.

Perhaps you could use something like:
Documents(templateDocumentName).Content.Copy
Documents(newDocumentName).PasteAndFormat (wdPasteDefault)

ill_comms
08-20-2008, 08:10 PM
Hi thanks for your help! I have fixed it. I believe it was because I had just activated the document not actually selected it, so the cursor for instance was not in the document.



Documents(templateDocumentName).Select
Selection.WholeStory
Selection.Copy
Documents(newDocumentName).Activate
Documents(newDocumentName).Select
Selection.WholeStory
Documents(newDocumentName).PasteAndFormat (wdPasteDefault)


Funny that the previous version work with XP not vista though?

Regards Hayden

macropod
08-21-2008, 12:23 AM
Hi Hayden,

All those Select/Selection statements and the Activate really slow the code down. It's much more efficient if you simply nominate the working ranges.

Give the code I posted a try - I'm not sure if the 2nd line will work, because 'PasteAndFormat' didn't exist in Word 2000 (which is what I'm using at the moment) so I can't test it.