Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2012, 02:46 PM
New Daddy New Daddy is offline Word counting macro for newly added portion? Windows Vista Word counting macro for newly added portion? Office 2003
Advanced Beginner
Word counting macro for newly added portion?
 
Join Date: Jan 2012
Posts: 90
New Daddy is on a distinguished road
Question Word counting macro for newly added portion?

I'd like to build a habit of writing at least a certain number of words per day. If I turn on the tracking, I can distinguish the newly added parts from the previous draft. Is it possible to count the words within such new addition using macro?
Reply With Quote
  #2  
Old 01-07-2012, 11:35 PM
macropod's Avatar
macropod macropod is offline Word counting macro for newly added portion? Windows 7 64bit Word counting macro for newly added portion? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi New Daddy,

You could use a macro like:
Code:
Sub WordCounter()
Dim oRev As Revision, i As Long
For Each oRev In ActiveDocument.Revisions
  If Int(oRev.Date) = Date And oRev.Type = wdRevisionInsert Then
    i = i + oRev.Range.ComputeStatistics(wdStatisticWords)
  End If
Next
MsgBox i & " words added today.", vbInformation
End Sub
Note that this only counts words in the body of the document - not anything in textboxes, headers, footers, etc.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-08-2012, 06:58 AM
New Daddy New Daddy is offline Word counting macro for newly added portion? Windows Vista Word counting macro for newly added portion? Office 2003
Advanced Beginner
Word counting macro for newly added portion?
 
Join Date: Jan 2012
Posts: 90
New Daddy is on a distinguished road
Default

This is great stuff! Works just as I wished too. Thanks!

Last edited by macropod; 12-09-2012 at 05:33 PM. Reason: Deleted unnecessary quote of entire post replied to
Reply With Quote
  #4  
Old 01-25-2012, 10:41 PM
New Daddy New Daddy is offline Word counting macro for newly added portion? Windows Vista Word counting macro for newly added portion? Office 2003
Advanced Beginner
Word counting macro for newly added portion?
 
Join Date: Jan 2012
Posts: 90
New Daddy is on a distinguished road
Default

Is there a way to make this macro count revisions in the footnotes as well?
I've been trying to combine .Footnotes and .Revisions with ActiveDocument, but it doesn't seem to work.

Last edited by macropod; 12-09-2012 at 05:33 PM. Reason: Deleted unnecessary quote of entire post replied to
Reply With Quote
  #5  
Old 01-26-2012, 01:16 AM
macropod's Avatar
macropod macropod is offline Word counting macro for newly added portion? Windows 7 64bit Word counting macro for newly added portion? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by New Daddy View Post
Is there a way to make this macro count revisions in the footnotes as well?
I've been trying to combine .Footnotes and .Revisions with ActiveDocument, but it doesn't seem to work.
Hi New Daddy,

Yes, the macro's scope can be expanded, but I'd rather not do it piecemeal:
Code:
Sub WordsForToday()
Application.ScreenUpdating = False
Dim Sctn As Section, oHdFt As HeaderFooter, lHdr As Long, lFtr As Long
Dim oEnt As Endnote, lEnt As Long, oFnt As Footnote, lFnt As Long
Dim oShp As Shape, lShp As Long
With ActiveDocument
  For Each oEnt In .Endnotes
    lEnt = lEnt + CountRevisions(oEnt.Range)
  Next
  For Each oFnt In .Footnotes
    lFnt = lFnt + CountRevisions(oFnt.Range)
  Next
  For Each Sctn In .Sections
    For Each oHdFt In Sctn.Headers
      If Not oHdFt.LinkToPrevious Then _
        lHdr = lHdr + CountRevisions(oHdFt.Range)
    Next
    For Each oHdFt In Sctn.Footers
      If Not oHdFt.LinkToPrevious Then _
        lFtr = lFtr + CountRevisions(oHdFt.Range)
    Next
  Next
  For Each oShp In .Shapes
    If Not oShp.TextFrame Is Nothing Then _
      lShp = lShp + CountRevisions(oShp.TextFrame.TextRange)
  Next
  MsgBox "Today's Word Count Statistics:" & vbCr & _
    "EndNotes - " & vbTab & lEnt & vbCr & _
    "Footnotes - " & vbTab & lFnt & vbCr & _
    "Headers - " & vbTab & vbTab & lHdr & vbCr & _
    "Footers - " & vbTab & vbTab & lFtr & vbCr & _
    "Shapes - " & vbTab & vbTab & lShp & vbCr & _
    "Other - " & vbTab & vbTab & CountRevisions(.Range)
End With
Application.ScreenUpdating = True
End Sub
 
Function CountRevisions(Rng As Range) As Long
Dim oRev As Revision, i As Long
For Each oRev In Rng.Revisions
  If Int(oRev.Date) = Date And oRev.Type = wdRevisionInsert Then
    i = i + oRev.Range.ComputeStatistics(wdStatisticWords)
  End If
Next
CountRevisions = i
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 01-26-2012, 09:56 AM
New Daddy New Daddy is offline Word counting macro for newly added portion? Windows Vista Word counting macro for newly added portion? Office 2003
Advanced Beginner
Word counting macro for newly added portion?
 
Join Date: Jan 2012
Posts: 90
New Daddy is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Hi New Daddy,

Yes, the macro's scope can be expanded, but I'd rather not do it piecemeal:
This is beautiful!
Thanks!
Reply With Quote
  #7  
Old 11-26-2012, 11:07 PM
New Daddy New Daddy is offline Word counting macro for newly added portion? Windows Vista Word counting macro for newly added portion? Office 2003
Advanced Beginner
Word counting macro for newly added portion?
 
Join Date: Jan 2012
Posts: 90
New Daddy is on a distinguished road
Default

It's been a while and I've been an ardent user of this macro.
But I just found that the footnote part under-counts the new words added. I counted manually to make sure, and there is a substantial under-counting.

Any idea why this is happening?

Last edited by macropod; 12-09-2012 at 05:32 PM. Reason: Deleted unnecessary quote of entire post replied to
Reply With Quote
  #8  
Old 11-26-2012, 11:46 PM
macropod's Avatar
macropod macropod is offline Word counting macro for newly added portion? Windows 7 64bit Word counting macro for newly added portion? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I've retested and an unable to generate an erroneous count.
Can you attach a document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 11-27-2012, 10:33 AM
New Daddy New Daddy is offline Word counting macro for newly added portion? Windows Vista Word counting macro for newly added portion? Office 2003
Advanced Beginner
Word counting macro for newly added portion?
 
Join Date: Jan 2012
Posts: 90
New Daddy is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I've retested and an unable to generate an erroneous count.
Can you attach a document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab.
Is there any chance that this same macro can behave differently in Word 2003 than in Word 2007 and later? The footnotes are fully counted in Word 2007 at work.

In any event, attached is a little sample whose tracked footnotes are not counted when the macro is run in Word 2003 but are counted in Word 2007.

Last edited by New Daddy; 11-27-2012 at 06:52 PM.
Reply With Quote
  #10  
Old 11-27-2012, 02:52 PM
macropod's Avatar
macropod macropod is offline Word counting macro for newly added portion? Windows 7 64bit Word counting macro for newly added portion? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try changing the Function to:
Code:
Function CountRevisions(Rng As Range) As Long
Dim i As Long, j As Long
With Rng
  For i = 1 To .Revisions.Count
    If .Revisions(i).Type = wdRevisionInsert Then
      j = j + .Revisions(i).Range.ComputeStatistics(wdStatisticWords)
    End If
  Next
End With
CountRevisions = j
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 11-27-2012, 09:09 PM
New Daddy New Daddy is offline Word counting macro for newly added portion? Windows Vista Word counting macro for newly added portion? Office 2003
Advanced Beginner
Word counting macro for newly added portion?
 
Join Date: Jan 2012
Posts: 90
New Daddy is on a distinguished road
Default

It's perfectly working now! Thanks!

Last edited by macropod; 12-09-2012 at 05:34 PM. Reason: Deleted unnecessary quote of entire post replied to
Reply With Quote
  #12  
Old 11-29-2012, 11:16 PM
New Daddy New Daddy is offline Word counting macro for newly added portion? Windows Vista Word counting macro for newly added portion? Office 2003
Advanced Beginner
Word counting macro for newly added portion?
 
Join Date: Jan 2012
Posts: 90
New Daddy is on a distinguished road
Default

In an attempt to be honest about my daily work progress, I added a couple of lines to the CountRevisions function so that it can subtract the number of words deleted, to give the total the net words added. But it doesn't work. It doesn't seem to be subtracting the number of words deleted.

Any idea why not? Could this be another Word 2003 issue?

Code:
Function CountRevisions(Rng As Range) As Long
Dim oRev As revision, i As Long, j As Long

With Rng
  For i = 1 To .Revisions.Count
    If .Revisions(i).Type = wdRevisionInsert Then
      j = j + .Revisions(i).Range.ComputeStatistics(wdStatisticWords)
    End If
    If .Revisions(i).Type = wdRevisionDelete Then
      j = j - .Revisions(i).Range.ComputeStatistics(wdStatisticWords)
    End If
  Next
End With

CountRevisions = j
End Function
Reply With Quote
  #13  
Old 11-30-2012, 12:10 AM
macropod's Avatar
macropod macropod is offline Word counting macro for newly added portion? Windows 7 64bit Word counting macro for newly added portion? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi New Daddy,

It's nothing to do with Word 2003. Rather, it's that ComputeStatistics(wdStatisticWords) doesn't count text marked as deleted. Here's a workaround:
Code:
Function CountRevisions(Rng As Range) As Long
Dim i As Long, j As Long, Str As String
With Rng
  For i = 1 To .Revisions.Count
    If .Revisions(i).Type = wdRevisionInsert Then
      j = j + .Revisions(i).Range.ComputeStatistics(wdStatisticWords)
    ElseIf .Revisions(i).Type = wdRevisionDelete Then
      Str = Trim(.Revisions(i).Range.Text)
      Str = Replace(Str, vbCr, " ")
      While InStr(Str, "  ") > 0
        Str = Replace(Str, "  ", " ")
      Wend
      j = j - (UBound(Split(Str, " ")) + 1)
    End If
  Next
End With
CountRevisions = j
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 12-09-2012, 04:49 PM
New Daddy New Daddy is offline Word counting macro for newly added portion? Windows Vista Word counting macro for newly added portion? Office 2003
Advanced Beginner
Word counting macro for newly added portion?
 
Join Date: Jan 2012
Posts: 90
New Daddy is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
It's nothing to do with Word 2003. Rather, it's that ComputeStatistics(wdStatisticWords) doesn't count text marked as deleted. Here's a workaround:
This new code worked fine and exactly as it was supposed to. Thank you very much.

But recently, it started to cause an '5852 run-time error', where the following code kicks in. This is really strange, because it was fine a few days ago. Any idea why?

If .Revisions(i).Type = wdRevisionInsert Then

Last edited by macropod; 12-09-2012 at 05:32 PM. Reason: Deleted unnecessary quote of portions of post replied to
Reply With Quote
  #15  
Old 12-09-2012, 05:31 PM
macropod's Avatar
macropod macropod is offline Word counting macro for newly added portion? Windows 7 64bit Word counting macro for newly added portion? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I'm not aware of anything in the code that could account for that. Perhaps re-starting Word (or even Windows) will resolve the problem. If not, try repairing the Office installation (via Help|Detect & Repair for Word 2003 & via Programs & Features > Microsoft Office > Change in the Windows Control Panel for Word 2010).

PS: There's no need to quote each previous post in its entirety every time you make a reply. Quote only when you need to and only those parts warranting it.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word counting macro for newly added portion? Counting Colors g48dd Excel 2 03-13-2011 09:28 PM
Added letters to body message kawzie Outlook 1 07-12-2010 11:00 AM
Word counting macro for newly added portion? Half hour added between tasks maruchi Project 1 06-17-2010 08:06 AM
Form field to automatically be added to header? razberri Word VBA 3 02-22-2010 03:48 PM
Word counting macro for newly added portion? Help! All Messages in Web Email deleted when account added to Outlook UrbanEast Outlook 2 07-17-2009 09:32 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft