Macro to count tracked insertions


P

phosphaenus

I've been looking for macros to count words inserted or modified, as
tracked using Track Changes. Note that I want to count inserted words
and words I've changed, but not deleted words. [Ideally I could decide
whether to count inserted punctuation or not, but this is a fine
point for the time being]

Anyway, on a Japanese newsgroup I found the following macro, posted by
Miyahn (Masataka Miyashita), and it seems to work very nicely.
However, for reasons I can't understand it seems to overcount by 5 or
10%, at least in my test document... I can't see any pattern to the
overcounting, and if anyone can explain it (and ideally resolve it)
I'd be delighted.

Sub CountInsertWords()
Dim aRev As Revision, I
For Each aRev In ActiveDocument.Revisions
If aRev.Type = wdRevisionInsert Then I = I +
aRev.Range.Words.Count
Next aRev
MsgBox I
End Sub
 
Ad

Advertisements

G

Guest

It may require a much more complex macro to return the number of words you
expect. Using your macro, some types of tracked changes will result in a
number that is higher than you may expect, for example:

If a word contains more than one change, each of the changes will count
(example: if the word "exitin" is changed to "exiStinG", it will count 2).

Some added characters that you do not regard as a word may be regarded as a
word by Word (example: a space followed by a hyphen, " -", was counted as 2
words during my test).

If you want to find out how the changes in your document are counted, you
could try replacing your macro by the one below while testing. The only
difference is that the version below displays a message box each time one or
more changed/added word strings are found. The message box shows the text
string found plus the number of words (as counted by the macro).

Sub CountInsertWords()
Dim aRev As Revision, I
For Each aRev In ActiveDocument.Revisions

If aRev.Type = wdRevisionInsert Then
I = I + aRev.Range.Words.Count

’Show message box
MsgBox "Text: " & aRev.Range.Text & vbCr & _
"Words: " & aRev.Range.Words.Count

End If

Next aRev
MsgBox I
End Sub

Hope this helps.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

Jay Freedman

The biggest problem with the original macro is that the Words
collection counts many kinds of punctuation marks (periods, question
marks, etc.) and each paragraph mark as a separate "word". Any of
those items that occur within tracked inserts will inflate the count
reported by the macro.

An alternative that doesn't have that drawback is to use the built-in
Tools > WordCount dialog. Calling .Execute on the dialog object lets
it count the number of words (and characters, sentences, etc.) in the
Selection without actually displaying the dialog. The drawback is that
it requires each insertion to be selected while the dialog is invoked,
so it takes a little extra machinery in the macro.

Here's my variant:

Sub CountInsertWords2()
Dim aRev As Revision, I As Integer, rgOrig As Range
Set rgOrig = Selection.Range
Application.ScreenUpdating = False
For Each aRev In ActiveDocument.Revisions
If aRev.Type = wdRevisionInsert Then
aRev.Range.Select
With Dialogs(wdDialogToolsWordCount)
.Execute
I = I + .Words
End With
End If
Next aRev
rgOrig.Select
Application.ScreenUpdating = True
MsgBox I & " words"
Set rgOrig = Nothing
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
P

phosphaenus

Very many thanks. I'm aware this is professional advice from
professional specialists: a rare privilege indeed. I'll look into your
very pertinent suggestions.

Thank you once more.
 
Joined
Jun 6, 2012
Messages
1
Reaction score
0
Hi all - I'm trying to use this macro (either one) with word 2007 but i keep receiving a run time error - anybody know how to help?
id really appreciate it! thanks!
 
Ad

Advertisements

Joined
Mar 7, 2016
Messages
1
Reaction score
0
Can someone explain step by step how to run the macro?
TIA!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top