![all word documents open debugger all word documents open debugger](https://i.stack.imgur.com/j4iok.png)
Clever, huh?Īfter we get the total number of pages for the first Word document we set the Saved property to True, which ensures that Word won’t ask us if we want to save changes to the document we then close document 1. The variable intPages will now be equal to 18 which – magically enough – also equals the total number of pages we’ve found so far.
![all word documents open debugger all word documents open debugger](https://code.visualstudio.com/assets/docs/getstarted/settings/settings-groups.png)
That means our next step is to add those 13 pages to the current value of intPages (5). Now, suppose we loop around and find a second Word document, this one containing 13 pages. That means our equation will look like this: intPages = 0 + 5 Let’s assume that document 1 has 5 pages. Why? Well, the first time through the loop intPages will be equal to 0 that’s because we haven’t assigned it a value yet. We take the number of pages in document 1, add that to the current value of intPages, and then assign the sum of those two numbers to intPages. What we’re doing here is determining the number of pages in our first document, something we do by calling ComputeStatistics and passing it the constant wdStatisticPages. With the document open we can then use the ComputeStatistics method to determine the number of pages in the file: intPages = intPages + objDoc.ComputeStatistics(wdStatisticPages)Īs you can see, we’re using a variable named intPages to keep track of the total number of pages. We can do this because the Name property gives us the complete path to the file.
ALL WORD DOCUMENTS OPEN DEBUGGER CODE
What then? Well, for starters, we use this line of code to open the document: Set objDoc = (objFile.Name) Let’s say that we do have a Word document. Yes, thanks for pointing that out: do not include the period when specifying a file extension in WMI. doc file extension): If objFile.Extension = “doc” Then The first thing we do inside that loop is check to see whether we’re working with a Word document (that is, a file with a. To begin with we set up a For Each loop to loop through the collection of files found in the folder C:\Scripts. Leave that line of code in or take it out that’s entirely up to you. However, this does give a visual cue that something is going on. That, by the way, is optional: we don’t need Word to be visible onscreen in order to add up the number of pages in the various Word documents. OK, so where were we? Oh, yeah: we create an instance of the Word.Application object (in this case, an instance of Word on the local computer) and then set the Visible property to True. However, if you want to work with Word documents on a remote computer you need to include the computer name as the optional second parameter to the CreateObject method: Set objWord = CreateObject(“Word.Application”, “atl-ws-01”) In the sample script we’re showing you today we’re working with Word documents on the local computer therefore we use this line of code to create an instance of Word: Set objWord = CreateObject(“Word.Application”)
![all word documents open debugger all word documents open debugger](https://www.greyhathacker.net/images/autoopen_macro.png)
Second, you need to create an instance of the Word.Application object on the remote machine. That enables us to connect to the WMI service on the remote computer.
![all word documents open debugger all word documents open debugger](https://dedicatedexcel.com/wp-content/uploads/2012/05/Open-Word-with-Excel-VBA-02.jpg)
First, you need to assign the name of that remote machine to the variable strComputer for example: strComputer = “atl-fs-01” In order to pull off that trick you need to do two things. Next we – what’s that? Can you perform this same task against a remote computer? As a matter of fact you can, provided that Microsoft Word is installed on that remote computer. Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”) After all, the world (or at least you, RA) was depending on him someone had to come up with a script that could give you a total page count for all the Word documents in a folder: Const wdStatisticPages = 2 The Scripting Son calmly got out of bed and went to school, like he always does.ĭespite the tears, and despite the beriberi, the Scripting Dad knew he had no choice but to come in to work. Of course, now that we think about it, all the screaming and crying seemed to emanate from the Scripting Dad, who didn’t want to go to work. All in all, it made for a very long and very tiring morning. Yesterday was the first day of school in the Lake Washington School District and, as you might expect, the scene in the Scripting Household was one of utter chaos: the kicking and screaming, the tears and tantrums, the sudden onset of beriberi, which made it impossible to leave the house. You’ll have to forgive the Scripting Guy who writes this column if he seems a little tired this morning. Hey, Scripting Guy! How can I get a total page count for all the Word documents in a folder?