PDA

View Full Version : switching between 2 docs with macro



sbraver
04-18-2013, 01:42 PM
Hello, All.
My issue is actually with a Word (2003) macro running on Windows 7.
I want my macro to start in whatever doc is the active window (which may vary from instance to instance), get some text from there and copy it into the clipboard, switch to the next document (which will always be called Document 2), paste the clipboard contents there (plus a return), then switch back to the original window, then stop the macro, awaiting the next macro call. I know how to do everything but the window switches. When I do it with the macro recorder, it inserts the name of the original document in quotes, which limits its generality.
Thanks for your help.

Mavila
04-20-2013, 08:56 PM
I would assign a variable to a newly created document, such as:

strNewDocNm = activedocument.name

Switching is then achieved by:

Documents(strNewDocNm).activate

The syntax might be off a bit as I'm going off of memory, but that is a method I use.

macropod
04-22-2013, 04:30 AM
You should not need to switch documents! Simply define and assign variables for the open documents, then simply reference them. For example:
Dim Doc1 As Document, Doc2 As Document
Set Doc1 = ActiveDocument
Set doc = Documents("Document2")
Doc1.Paragraphs(3).Range.Copy
Doc2.Range.Characters.Last.Paste
'or even:
Doc2.Range.Characters.Last.InsertAfter Doc1.Paragraphs(3).Range.Text