Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Counting Open Document Windows.

Counting Open Document Windows

by Allen Wyatt
(last updated September 19, 2014)

2

Christine is writing a macro and needs to figure out how many document windows are open. The traditional means of doing this is to use the Windows.Count property, in the following manner:

iNumWindows = Application.Windows.Count

When executed, iNumWindows will contain the number of open document windows in Word. The problem is that it returns a count of any window that Word may consider a document, even those that contain e-mails.

As far as we can determine, there is no way around this inclusive behavior of Word. If a person is using Word as their e-mail editor, and they open an e-mail or two, those windows are considered document windows by the program. Granted, they are not documents destined for a disk file or for the printer, but they are documents nonetheless.

In addition, there is no other flag that we could locate that would allow one to differentiate between a regular document window and an e-mail message window. If such a flag were available, then someone could easily check the windows and produce their own count of documents vs. e-mail messages.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (10516) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Counting Open Document Windows.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Discovering Dependent Workbooks

When you starting linking information from one workbook to another, those workbooks become dependent on each other. ...

Discover More

Custom Menu Stays, but Options Disappear

It can be disconcerting when a custom menu you've used for years suddenly loses all the options it previously contained. Here ...

Discover More

Adding a Drawing to Your Spreadsheet

Numbers alone do not define spreadsheets. Google Drive allows you to also add drawings to a spreadsheet, which can be very ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Determining a Random Value

If you need to determine a random value in a macro, you can do so using the Rnd function. This tip presents the syntax and ...

Discover More

Clearing the Undo Stack in a Macro

When writing a macro, you may need a way to clear the undo stack. This can be done with a single command, as described in ...

Discover More

Converting Strings to Numbers

When creating macros, you often need to convert a text string that contains numbers into actual numeric values. You do this ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 2?

2014-09-19 10:33:09

Scott Renz

You would have to add two lines if you have and unnamed document.
And it would be different as well depending on if you want to count unnamed documents as documents.

Sub ShowWindowcounts()
Dim w As Windows
Set w = Application.Windows
n = 0
For i = 1 To w.Count
If InStrRev(w.Item(i).Caption, ".") > 0 Then
If LCase(Mid(w.Item(i).Caption, InStrRev(w.Item(i).Caption, "."), 4)) = ".doc" Then n = n + 1
End If
Next i
MsgBox ("You have " & n & " open document windows out of " & w.Count & " windows total.")
Set w = Nothing
End Sub

Sub ShowWindowcounts()
Dim w As Windows
Set w = Application.Windows
n = 0
For i = 1 To w.Count
If InStrRev(w.Item(i).Caption, ".") = 0 Then
n = n + 1
ElseIf LCase(Mid(w.Item(i).Caption, InStrRev(w.Item(i).Caption, "."), 4)) = ".doc" Then n = n + 1
End If
Next i
MsgBox ("You have " & n & " open document windows out of " & w.Count & " windows total.")
Set w = Nothing
End Sub


2014-09-19 10:19:39

Scott Renz

You could make a macro something like this:
Sub ShowWindowcounts()
Dim w As Windows
Set w = Application.Windows
n = 0
For i = 1 To w.Count
If LCase(Mid(w.Item(i).Caption, InStrRev(w.Item(i).Caption, "."), 4)) = ".doc" Then n = n + 1
Next i
MsgBox ("You have " & n & " open document windows out of " & w.Count & " windows total.")
Set w = Nothing
End Sub


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.