|
Gross line counting in Word 97
Months ago I outlined how to make a character counting macro in for Microsoft Word 97. At the time that was the only line counting macro I knew how to make in Word. Well things have changed!
Basics
In WP8 or WP51 when I want to count gross lines I write a loop into the macro and then have the macro test if the line is blank or not. If the line is blank the cursor moves down a line and tests the next line. If there is something on the line the macro adds one to a variable and moves the cursor down to the next line. When the cursor gets to the end of the document the loop stops and the macro displays the number in the count variable.
I'm by no means a Word Visual Basic programming expert so I've had to find an alternate way to count gross lines in Word. Good thing for us Word does allow you to access the total number of lines in a document with a macro command:
ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)
As I said earlier though this is the total number of lines so blank lines are counted in the total. We have to remedy that.
Removing blank lines from a document
Now that we have a command that gives the total number of lines in a document we have to make sure that blank lines aren't going to be counted. The easiest way to do this is to remove them from the document. Even though this sounds like a lot of work it's actually quite easy.
Open the document you want to count and start recording a macro by opening the Tools Menu | Macro > Record New Macro... Here you can name the macro. I named mine GrossLineCounter. If you put in spaces Word gives you an error. Assign the macro to the keyboard by clicking the Keyboard button. Choose a key combination that will run the macro. (I put mine on Alt - C.) Click the Assign button. Then Close.
Now you should be back in your document. Press Ctrl-A to select everything in the document. Then Ctrl-C to copy everything to the clipboard. Open a new blank document and paste the contents of the document to be counted by pressing Ctrl-V.
The new document should look exactly like the document you are counting. Press Ctrl-Home to get to the very top of page 1. Now the fun starts. Open the Edit menu and choose Replace... In the Find What field type: ^p^p. This tells Word to search for two paragraph marks (the same as hard returns for us WP people). Then in the Replace with field put one paragraph mark, ^p. Click the Replace all button about 8 times. This will ensure that you've removed all the blank lines even if you have a full page of only paragraph marks. After that click the Close button.
Edit that macro
At this point stop recording the macro by pressing the blue square in the little dialog in the upper left of your screen. Mine says StopR. Another way to do this is to open the Tools Menu | Macros > and choose Stop Recording...
From here you could just open the Tools menu and choose Word Count... to see the total number of lines, but let's be more elegant than that.
Edit the macro you've just created by opening the Tools Menu and choosing Macro > Macros. Highlight GrossLineCounter (or whatever you named it) and click the Edit button.
You should see something similar to this:
Sub GrossLineCounter()
'
' GrossLineCounter Macro
' Macro recorded 04/18/00 by Mike DeTuri
'
Selection.WholeStory
Selection.Copy
Documents.Add
Selection.Paste
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll Selection.Find.Execute Replace:=wdReplaceAll Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Add the following lines to the macro right before the End Sub line:
Dim glines As String
glines=Str(ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)) + " lines"
MsgBox "This document contains: " + glines, vbOKOnly, "Gross Line Count"
ActiveWindow.Close
Selection.HomeKey Unit:=wdStory
The first lines we added defines the variable "glines" as a string of characters, which is necessary to display the line count total in a message box. The second line assigns the number of lines in the current document plus the word "lines" to the variable "glines". The third line displays a message box with an Okay button that shows us the value of the variable glines.
The last two lines we added close the secondary document and place you at the top of the document you counted.
The completed gross line count macro
The finished macro should look like this:
Sub GrossLineCounter()
'
' GrossLineCounter Macro
' Macro recorded 04/18/00 by Mike DeTuri
'
Selection.WholeStory
Selection.Copy
Documents.Add
Selection.Paste
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Dim glines As String
glines = Str(ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)) + " lines"
MsgBox "This document contains: " + glines, vbOKOnly, "Gross Line Count"
ActiveWindow.Close
Selection.HomeKey Unit:=wdStory
End Sub
Copyright 2000 by Mike DeTuri (reprinted from Computer Solutions Vol. 2, Issue 1)
|