none
How to determine the index of the ActiveDocument in Word? RRS feed

  • Question

  • Microsoft documentation says you can select one of several documents open in Word by using either a document name or the index number of the document. They give this example to return the first document in the Documents collection -- Dim Doc as Document = Documents(1)

    But I can't find any index property for the Document object! So how do I determine the index number of the ActiveDocument in order to use it as in the above example?

    I want this so I can do something like this:

    Dim n as integer = ActiveDocument.Index

    n = n + 1

    if n > Documents.count then n = 1

    Documents(n).Activate

    I know I could use something like ActiveWindow.next to get the next "window", but I don't want to use the Window object so I won't be iterating though several windows of one document before I get to the next document.


    Robert Homes

    Saturday, July 4, 2015 5:26 AM

Answers

  • You can, of course, iterate through the documents with code like:

    Dim i As Long
    For i = 1 To Documents.Count
      Documents(i).Activate
    Next

    You could leverage that to activate any document other than the ActiveDocument, with code like:

    Dim i As Long, j As Long
    j = Documents.Count
    For i = 1 To j
      If Documents(i).FullName = ActiveDocument.FullName Then
        If i < j Then
          Documents(i + 1).Activate
        Exit For
        End If
      End If
    Next

    IMHO, though, it might be better to set a reference to each document (e.g. upon opening), in which case you can activate any of them without going through loops or worrying about whether their index # might have changed.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, July 4, 2015 6:33 AM
  • Hi Robert

    I've never tested it or researched how Word assigns and manages the Index number, but I wouldn't want to bet that Word won't change the Index value of a document during a session.

    Paul's suggestion about doing your own document "tracking" is likely more reliable/predictable. An Add-in can monitor both opening and creating documents on the part of the user (DocumentOpen and NewDocument events), as well as closing (DocumentClose). So it would be relatively easy for you to use an array, collection or similar to "hold" a Document object plus an index number for each document.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, July 4, 2015 3:48 PM
    Moderator

All replies

  • You can, of course, iterate through the documents with code like:

    Dim i As Long
    For i = 1 To Documents.Count
      Documents(i).Activate
    Next

    You could leverage that to activate any document other than the ActiveDocument, with code like:

    Dim i As Long, j As Long
    j = Documents.Count
    For i = 1 To j
      If Documents(i).FullName = ActiveDocument.FullName Then
        If i < j Then
          Documents(i + 1).Activate
        Exit For
        End If
      End If
    Next

    IMHO, though, it might be better to set a reference to each document (e.g. upon opening), in which case you can activate any of them without going through loops or worrying about whether their index # might have changed.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, July 4, 2015 6:33 AM
  • That doesn't quite accomplish what I want. If you've got more than one document open, you don't want to always activate the first one. You want to activate the one following the one you're already on and only loop around to the first one if you've reached the last one.

    Robert Homes

    Saturday, July 4, 2015 2:49 PM
  • Hi Robert

    I've never tested it or researched how Word assigns and manages the Index number, but I wouldn't want to bet that Word won't change the Index value of a document during a session.

    Paul's suggestion about doing your own document "tracking" is likely more reliable/predictable. An Add-in can monitor both opening and creating documents on the part of the user (DocumentOpen and NewDocument events), as well as closing (DocumentClose). So it would be relatively easy for you to use an array, collection or similar to "hold" a Document object plus an index number for each document.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, July 4, 2015 3:48 PM
    Moderator
  • The second code snippet does all of that, except for going back to the first one. Adding that in is trivial - it even makes for simpler code:

    Dim i As Long, j As Long
    j = Documents.Count
    For i = 1 To j
      If Documents(i).FullName = ActiveDocument.FullName Then
        Documents(i Mod j + 1).Activate
        Exit For
      End If
    Next

    I must reiterate, though, that this is not a reliable way of activating the correct document in all circumstances.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, July 4, 2015 9:50 PM