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

Adjusting Small Caps Text

If you use small caps text in a document, you know that there are several steps involved in properly formatting the text. ...

Discover More

Creating a Document Font List

If you want a list of all the fonts used in a document, the answer isn't as simple as you may think. This tip uses macros ...

Discover More

Lotus Grouped Worksheets

Not all spreadsheet programs are created equal; there are some things that can be done in others that can't be done in Excel. ...

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 the Upper Bounds of an Array

When working with variables in a macro, you may need to know the upper boundary dimension for an array. This can be ...

Discover More

Moving to the Start or End of the Real Document

The main body of your text is only one part of what makes up the entire document. Documents can consist of other elements, as ...

Discover More

Turning Off Screen Updating

When working with macros, you can often speed up processing by turning off the updating of the screen. Best news is that it ...

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 8Mpixels. 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 7 - 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.