I recently had a project containing a large number of Word documents of various sizes and the Project Manager wanted to know if the documents were “Letter size” or “Legal size” and whether they were “Portrait” or “Landscape”. I had a couple options – I could either open each of the documents and write down the information or I could write some VBA code to do it for me. I decided to write the code.
The code below will get all of the “docx” file names in the specified directory. It will then open each file, get the document name, document width and document height and store this information in variables before closing the document. To minimize distractions and speed up execution I turned off “Alerts” and “Screen updating” before doing anything else and the last thing I did was to turn them back on. I wanted to make a clean, easily readable document so I used tabs (vbTab) and set the tabs in the document to be right-aligned. I knew that I was dealing with only two (2) paper sizes – either 8 1/2 x 11 (Letter) or 8 1/2 x 14 (Legal) – so it simplified the code considerably. If there is sufficient interest in adding other sizes I will work out the code and write a follow-up – let me know.
The first thing I do is to have the code navigate to the desired folder using ChangeFileOpenDirectory and supply it with the variable holding the path. The next step is to get a list of files in the directory and assign them to a variable, docName –
docName = Dir("*.docx"). Using a “Do While….” loop I go through the directory list, opening each document as I go. Once I get the information I need I close the document. You will notice I am using a function called “PointsToInches” – that is a built-in Word function that I chose to use but feel free to leave it out. If you don’t use it you will get the width and height in “points” and you will have to adjust your numbers (there are 72 points to 1 inch.)
Once I have the name, height and width I compare the width and height – if the document is wider than high I know it is “Landscape”, otherwise it is “Portrait”. If the document is “Landscape” I evaluate the width to determine if the document size is “Letter” or “Legal”. I do the same with a “Portrait” document except I evaluate height to determine the page size. The last thing I do is to write the information to my master document using
Selection.TypeText docName & ": " & vbTab & docOrient & vbTab & docSize & vbCrLf. I then move on to the next document by executing
docName = Dir. If you forget this line of code you will get stuck in an infinite loop!
Public Sub getPageSize()
Dim docName As String, docPath As String
Dim docOrient As String, docSize As String
Dim intWidth As Single, intHeight As Single
Application.DisplayAlerts = wdAlertsNone
Application.ScreenUpdating = False
docPath = "YOUR PATH NAME GOES HERE!"
docName = Dir("*.docx")
Do While docName <> vbNullString
intWidth = PointsToInches(Selection.PageSetup.PageWidth)
intHeight = PointsToInches(Selection.PageSetup.PageHeight)
If intWidth > intHeight Then
docOrient = "Landscape"
If intWidth = 14 Then
docSize = "Legal"
docSize = "Letter"
docOrient = "Portrait"
If intHeight = 14 Then
docSize = "Legal"
docSize = "Letter"
Selection.TypeText docName & ": " & vbTab & docOrient & vbTab & docSize & vbCrLf
docName = Dir
Application.ScreenUpdating = True
Application.DisplayAlerts = wdAlertsAll
That’s all you need! If you add this code to your Normal.dotx template it will always be available to you. To run the code you simply open a New document, update the path in the code and run from within the new document. It doesn’t get much easier. There are a lot more that can be done with this code, such as using a dialog box to get the path instead of hard-coding, adding more paper sizes (as mentioned above) and more statistics about the document like page count, paragraph count, word count, etc….
If you would like to know more about this project or have one you would like to discuss, please write to me at email@example.com.