Obtain the index number of the active document.

D

drbobsled

I know how to get the count of active documents and the active document name.
I'd like to extract the index number of the active document.
Any suggestions?

TIA
 
J

Jean-Guy Marcil

drbobsled was telling us:
drbobsled nous racontait que :
I know how to get the count of active documents and the active
document name. I'd like to extract the index number of the active
document.
Any suggestions?

Try with:

Dim MyIndex As Long

MyIndex = Application.ActiveWindow.Index


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jonathan West

drbobsled said:
I know how to get the count of active documents and the active document
name.
I'd like to extract the index number of the active document.
Any suggestions?


Documents.Count returns the number of open documents

The active documnent is Document(1) by definition. The index numbers change
depending on the order in which they were activated.
 
H

Helmut Weber

Hi everybody,
The active document is Document(1) by definition.

hmm...
Could this vary with the operating system and the Word version?
Here and now
Application.ActiveWindow.Index
returns 4 e.g., as Jean-Guy said.
Or am I missing something?
The index numbers change depending on the order in which they were activated.

hmm... again.

Here with Windows 98 and Word XP the list of open documents
is sorted in alphabetical order. Though I think I remember
that this is different with NT and Windows 2000.

Can't check it right now.

Cheers

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jonathan West

Hi Helmut,

Further research shows we were both wrong!

First of all, there is a distinction to be made between the index number of
the active document(which is what the original question asked for) and the
index number of the active window. For instance, a document might be open in
more than one window, and each window would have a different index number.

It appears that Document(1) is by definition the most recently *opened*
document, not necessarily the Active Document. If you have multiple
documents open and switch between them, the ActiveDocument changes, but
Document(1) does not.

You can find out the current index number of the active document (as apposed
to active window. The foillowing code puts the index number into the
variable iIndex

Dim iIndex as Long
Dim i As Long
For i = 1 to Documents.Count
If Documents(i).FullName = ActiveDocument.FullName Then
iIndex = i
Exit For
End If
Next i

However, Document(1) does change when a new document is opened or created.
Therefore, grabbing the index number of the document is not a reliable way
of keeping track of it. The reliable way of keeping track of a document is
to assign an object variable to it like this

Dim oDoc As Document
Set oDoc = ActiveDocument

Subsequently, and reference to oDoc is the reference to the document that
was assigned, and this does not change when additional documents are opened
or closed. Changes can be made to the document by using oDoc in just the
same way as you would normally use ActiveDocument, without having to
activate the document and bring it to the foreground.
 

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