Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

Document( ).Activate

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 10 Jan 2005 20:58 GMT
I have an open document.  I then use VBA to open a second document

Sub Test()

Dim SecondDocument As Document

Set SecondDocument = Documents.Open(FileName:="C:\Word List.doc")

'Why do I have to use (2) below to activate the first document?  I
seems like the original opened document would be (1).
Documents(2).Activate

End Sub
Alex Ivanov - 10 Jan 2005 21:22 GMT
When you open the SecondDocument it becomes ActiveDocument, which always has
index of 1.
Documents collection is ordered in a way that topmost open document has the
lowest index (1), second in a stack (2) and so forth.

You can't reasonably depend on the document index as it will change when you
activate another document.

Signature

Please reply to NG only. This email is not monitored.
Alex.

>I have an open document.  I then use VBA to open a second document
>
[quoted text clipped - 9 lines]
>
> End Sub
Jay Freedman - 10 Jan 2005 21:31 GMT
>I have an open document.  I then use VBA to open a second document
>
[quoted text clipped - 9 lines]
>
>End Sub

Hi Greg,

Don't use Documents() at all! That's the point of assigning documents
to Document variables -- do this instead:

Sub Test()

Dim FirstDocument As Document, SecondDocument As Document

Set FirstDocument = ActiveDocument
Set SecondDocument = Documents.Open(FileName:="C:\Word List.doc")

FirstDocument.Activate

End Sub

Even better, use the properties of the two Document variables to do
your manipulations, and avoid activating any documents at all. The
classic situation is copying or cutting some piece of text in the
first doc and pasting it into the second doc. Instead, once you set up
Range objects in the two documents (usually by Find in the first doc,
and collapsing to the end of the second doc), all you need is

  DestRange.FormattedText = SourceRange.FormattedText

No activation needed! Bonus, if the user has anything on the
clipboard, you don't disturb it.

BTW, I don't have an answer to the original question, why the indexes
in the Documents collection aren't in the expected order. <shrug>

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org
Greg Maxey - 11 Jan 2005 00:40 GMT
Alex, Jay,

Copy all.  Thanks for the explanation and tips.

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

>> I have an open document.  I then use VBA to open a second document
>>
[quoted text clipped - 40 lines]
> BTW, I don't have an answer to the original question, why the indexes
> in the Documents collection aren't in the expected order. <shrug
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.