#1
|
|||
|
|||
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. |
#2
|
|||
|
|||
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 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word Count | Richtriebe | Word | 1 | 04-20-2013 11:09 AM |
Word Count Macro | bpanda | Word VBA | 1 | 01-11-2013 06:51 PM |
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 |