Switching between documents and windows

G

G Teachman

Hello,

I want to cut and paste from one active document to another and keep the
first document window on top.

Code I'm working with:

Dialogs(wdDialogFileOpen).Show
' user selects the Main.docx file
' Main.docx is activated by default
' select and copy stuff from Main.docx
' open Target.docx via
Documents.Open FileName:="""Target.docx""" 'etcetera
' Target.docx is now active and it's window is on top
' paste stuff

Now I want to make Main.docx activated and it's window on top.
I can activate it easily enough by Windows("Main.docx").Activate
But I can't figure out how to make it the top window, without physically
clicking on it.

I could reverse the order of opening the files making Main.docx's window on
top. But I have other times when I can't control what window is on top by
the order in which the files were opened.

I would appreciate any help in showing me how to bring a window in the
background to the top of the pile.

Thanks,
 
F

Fumei2 via OfficeKB.com

If what you actually want to do is copy contents from one document to another
you do not ever need to do ANYActivate, nor use the window.

Use Document objects.

Dim SourceDoc As Document
Dim TargetDoc As Document

Set SourceDoc = Documents.Open Filename:="path_to_doc"

' or use ActiveDocument if it is open

Set TargetDoc = Documents.Open FileName:="Target.docx"

True, at this point TargetDoc is Active, but it does not matter. You can
grab whatever you want from SourceDoc even though it is not Active, and put
that into TargetDoc. For example:

TargetDoc.Sections(1).Footers(1).Range = _
SourceDoc.Sections(1).Footers(1).Range

This makes the Section(1).Footer(1) of TargetDoc the same as Section(1).
Footer(1) of SourceDoc.


TargetDoc.Sections(1).Range = SourceDoc.Sections(1).Range

This makes TargetDoc Sections(1) the same as SourceDoc Sections(1).


SourceDoc.Tables(2).Range.Copy

TargetDoc.Bookmarks("PutTableHere") _
.Range.PasteAndFormat (wdFormatOriginalFormatting)

This copies the second table of SourceDoc and inserts it at the bookmark
"PutTableHere" in TargetDoc.

All done without ever dealing with what document is Active, or what windows
is on top. It uses Document objects.
 

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