Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-10-2013, 10:42 PM
bpanda bpanda is offline A recurring word count macro? Mac OS X A recurring word count macro? Office for Mac 2011
Novice
A recurring word count macro?
 
Join Date: Jan 2013
Posts: 3
bpanda is on a distinguished road
Default A recurring word count macro?

I managed to copy this macro from a website (forgotten which one):

Sub Section2WordCount()
' Section2WordCount Macro
' This macro counts the number of words in Section 2
' and prints it at the Bookmark named "WordCount"
' Counting is done by Format(.Sections(2).Range.ComputeStatistics(wdStat isticWords))
' which filters the command through the WordCount subroutine

Dim oRange As Word.Range
Dim sBookmarkName As String
Dim sTemp As String

sBookmarkName = "WordCount"
With ActiveDocument
sTemp = "( " + Format(.Sections(2).Range.ComputeStatistics(wdStat isticWords)) + " words)"
Set oRange = .Bookmarks(sBookmarkName).Range
oRange.Delete
oRange.InsertAfter Text:=sTemp


.Bookmarks.Add Name:=sBookmarkName, Range:=oRange
End With

End Sub

It's a really cool macro. Does anyone here know how to modify this macro so that it will do a word count for every section in a Word document and print the results at the end of the section?

Many thanks.
Reply With Quote
  #2  
Old 06-11-2013, 07:17 AM
gmaxey gmaxey is offline A recurring word count macro? Windows 7 32bit A recurring word count macro? Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub SectionWordCount()
Dim lngIndex As Long
Dim oRng As Word.Range
Dim strBMName As String
Dim strCount As String
  With ActiveDocument
    For lngIndex = 1 To .Sections.Count
      strCount = "(" + Format(.Sections(lngIndex).Range.ComputeStatistics(wdStatisticWords)) + " words)"
      strBMName = "WordCount" & "_" & lngIndex
      On Error Resume Next
      Set oRng = .Bookmarks(strBMName).Range
      If Err.Number <> 0 Then
        Set oRng = .Sections(lngIndex).Range
        oRng.Collapse wdCollapseEnd
        If Asc(oRng.Characters(1).Previous) = 12 Then
          oRng.Move wdCharacter, -1
        End If
      Else
        oRng.Delete
      End If
      oRng.Text = vbCr & strCount
      oRng.MoveStart wdCharacter, 1
      .Bookmarks.Add Name:=strBMName, Range:=oRng
    Next
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Count Richtriebe Word 1 04-20-2013 11:09 AM
A recurring word count macro? Word Count Macro bpanda Word VBA 1 01-11-2013 06:51 PM
A recurring word count macro? Where did live word count go? Tango Mike Word 3 08-01-2012 09:34 PM
*Word 2007 Macro for Character Count gbartlet Word 0 07-21-2010 11:12 AM
FInd recurring words in Word 2003 NJ007 Word 4 01-25-2010 03:11 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:38 AM.


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