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:
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.
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!
In some documents Roman numerals might be used quite a bit. If you ever want to change the Roman numerals to their Arabic ...
Discover MoreWhen you distribute documents to other people, you may want those documents to have associated macros that the reader can ...
Discover MoreNeed to run one macro from within another macro? You can easily do it by using the Run method of the Application object, ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-03-04 16:12:19
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
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
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.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments