Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, and 2016. 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 November 4, 2017)

4

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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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, 2010, 2013, and 2016. 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

Creating a Multi-Worksheet Report

It is not uncommon to use Excel to print out regular reports. If your report needs to span multiple worksheets, here's ...

Discover More

Positioning a Column on the Screen

If you have static columns and dynamic columns on the screen, you may want the dynamic columns to always show a ...

Discover More

Calculating a Future Date

Need to figure out a date a certain number of days, weeks, months, or years in the future? It's easy to do using the ...

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)

Deleting a Macro

Macros are often created to accomplish a specific task, after which they are no longer needed. If you need to delete a ...

Discover More

Determining the Month of the Year

Your macro code may need to determine the month of the year represented by a particular date. You can find the desired ...

Discover More

Adding Automatic Time Stamps

Your computer knows the current date and time, and Word provides ways you can get that date and time into your document. ...

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}] (all 7 characters, in the sequence shown) 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 two less than 7?

2022-03-04 16:12:19

Tomek

There is a problem I encounter frequently when opening several documents from File Explorer (rather than from within Word). If I select multiple document files then press enter (or right-click>open) they do not open in one Word instance. Sometimes a few of them will be grouped but virtually always there are several instances of the word running. I just opened 6 docx files this way and there are five instances of Word running, one of them with two documents and the rest with a single document. This results in the Word windows not being aware of the others, not counting them, and also not closing them if I select close-all from my QAT. It does not matter if I use
iNumWindows = Application.Windows.Count
or
iNumWindows = Application.Documents.Count

The result is just count of 1, or in one instance of 2, while actually there are 6 documents open! Does anybody else have this problem.


2022-03-04 10:06:59

Andrew

James, you can test if the current window is protected with this:

If ActiveProtectedViewWindow Is Nothing Then
' The window is unprotected.
Else
' The window is protected.
End if


2022-03-03 06:56:53

James Martin

This is a problem I have encountered with a minor macro I have created to replace ">>>File>>>Close". In particular it does not seem possible to detect a protected Word file (doc/docx/rtf/odt) that is downloaded from a browser for a client system in the Active Window. I have had to use a lot of trial and error to get to this Macro X, an X on the Quick Access Tool Bar, to work. As long as a file is saved or not amended from download, pressing this Macro X will close down the Active Document only. I struggled to do this as Counting protectedview only counts all open protected view documents. I also think that a standard document count also counts recently closed documents in the Recently Closed list if you right click on Word in the Taskbar. You just have to accept this and trap what you want with logic. I had to resort to using "On Error Go To" to stop the macro breaking. As you can see in my Macro X, the end product is that it will resolve >File>Close in 3 possible outcomes:
Active Document is Protected View
Active Document is not protected view
Word App open and no Active Document (Macro X QAT pressed in error)

On very rare occasions I have had vba code breaks, but this is maybe a a few in several thousand uses of this macro so far. I have deliberately not included save current document prior to close, as this is not how I work and by default you need to have autonomy over each file, so a save prompt is triggered by Word App independent of the Macro if this occurs. It would be too confusing for other staff if this also saved.

This Macro X saves me 2% of my standard work hours if I use it 53 times a day (saving 10 seconds each time), in truth I use this 100s of times a day, as it is quicker than closing down the Word App in the top right corner (I had to sort out duplicate Word App openings in some of my macros and created this Macro after doing this debugging).

Sub WordFileClose()
'SOURCE: jam61mar@gmail.com

'not sure why ScreenUpdating goes lowercase in my Word App
Application.screenupdating = False

Dim WrdPV As ProtectedViewWindow

'If a file is Protected View, Make it Editable
If Application.ProtectedViewWindows.Count > 0 Then
'if protected view file is not active window
On Error GoTo Stage2
ActiveProtectedViewWindow.Edit
End If

Stage2:

'If Macro X is pressed in error with no file in Open Word App
On Error GoTo Terminate
'if Active window has a document close it
Application.activedocument.Close
Exit Sub

Terminate:

Application.screenupdating = True
End Sub

FYI I have duplicated this Macro X for Excel, you can see that my screenupdating in Excel works whereby I probably have a reference error in Word as it is not working. I am betting the xlsb popup error in Excel, but I just click "read-only" for this.

Sub ExcelFileClose()
'SOURCE: jam61mar@gmail.com

Application.ScreenUpdating = False

Dim WrdPV As ProtectedViewWindow

'If a file is Protected View, Make it Editable
If Application.ProtectedViewWindows.Count > 0 Then
'if protected view file is not active window
On Error GoTo Stage2
Application.ActiveProtectedViewWindow.Edit
End If

Stage2:

'If Macro X is pressed in error with no file in Open Excel App
On Error GoTo Terminate
'if Active window has a document close it
Application.ActiveWorkbook.Close
Exit Sub

Terminate:

Application.ScreenUpdating = True
End Sub


2017-11-04 13:23:17

Art Osgatharp

If the user were to have one document open and then use View...New Window to open another window for that document, the windows count will be 2 even though they display the same document. Note that when you open a new window on a document, Word adds a colon + the window number to the displayed name of the document at the top of the screen, for example, "My Document.docx:2". Any macro running in that situation has to ensure it operates against the correct window, since the cursor position could be different for each window.

If your intent is to count documents rather than windows, it might be more effective to use "iNumWindows = Application.Documents.Count" instead. Word will not count an open VBA editor window as a document using this technique, whereas Application.Windows.Count will count an open VBA editor as one of the windows.


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.

Videos
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.