wordcount macro for tables and brackets

N

Nikeel

Hello
I have a macro which excludes words inside tables and brackets from the
general word count in a document. The problem is the macro is not in the
right format (the writing appears red in Visual Basic Editor). Does anyone
know the right format for the macro or how I can get it to be right?
Any help is greatly appreciated, many thanks.
The macro is as follows:
Public Sub StatisticsExcludingTables() Dim tblItem As Word.Table Dim
rngReplace As Word.Range Dim rngFound As Word.Range Dim lngCharacters As Long
Dim lngCharactersWithSpaces As Long Dim lngFarEastCharacters As Long Dim
lngLines As Long Dim lngPages As Long Dim lngParagraphs As Long Dim lngWords
As Long ' Get the totals for the document With ActiveDocument lngCharacters =
..ComputeStatistics(wdStatisticCharacters) lngCharactersWithSpaces = _
..ComputeStatistics(wdStatisticCharactersWithSpaces) lngFarEastCharacters = _
..ComputeStatistics(wdStatisticFarEastCharacters) lngLines =
..ComputeStatistics(wdStatisticLines) lngPages =
..ComputeStatistics(wdStatisticPages) lngParagraphs =
..ComputeStatistics(wdStatisticParagraphs) lngWords =
..ComputeStatistics(wdStatisticWords) End With ' Subtract out the table
statistics For Each tblItem In ActiveDocument.Tables With tblItem.Range
lngCharacters = lngCharacters - _ .ComputeStatistics(wdStatisticCharacters)
lngCharactersWithSpaces = lngCharactersWithSpaces - _
..ComputeStatistics(wdStatisticCharactersWithSpaces) lngFarEastCharacters =
lngFarEastCharacters - _ .ComputeStatistics(wdStatisticFarEastCharacters)
lngLines = lngLines - .ComputeStatistics(wdStatisticLines) lngPages =
lngPages - .ComputeStatistics(wdStatisticPages) lngParagraphs = lngParagraphs
- _ .ComputeStatistics(wdStatisticParagraphs) lngWords = lngWords -
..ComputeStatistics(wdStatisticWords) End With Next ' Search document for
parenthesised words Set rngReplace = ActiveDocument.Content With
rngReplace.Find .ClearFormatting .Replacement.ClearFormatting .Text =
"\(<*>\)" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format =
False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False
..MatchSoundsLike = False .MatchWildcards = True ' Find all occurrences in the
document Do While .Execute ' Exclude statistics the words in parenthesis Set
rngFound = rngReplace.Duplicate With rngFound lngCharacters = lngCharacters -
_ .ComputeStatistics(wdStatisticCharacters) lngCharactersWithSpaces =
lngCharactersWithSpaces - _
..ComputeStatistics(wdStatisticCharactersWithSpaces) lngFarEastCharacters =
lngFarEastCharacters - _ .ComputeStatistics(wdStatisticFarEastCharacters)
lngWords = lngWords - .ComputeStatistics(wdStatisticWords) End With ' Setup
range to continue the search after ' the text that we just found
rngReplace.Collapse wdCollapseEnd Loop End With MsgBox "Pages: " & lngPages &
vbCr & _ "Paragraphs: " & lngParagraphs & vbCr & _ "Lines: " & lngLines &
vbCr & _ "Words: " & lngWords & vbCr & _ "Characters: " & lngCharacters &
vbCr & _ "Characters with spaces: " & lngCharactersWithSpaces & vbCr & _ "Far
east Characters: " & lngFarEastCharacters End Sub
 
D

Doug Robbins - Word MVP

It should be:

Public Sub StatisticsExcludingTables()

Dim tblItem As Word.Table
Dim rngReplace As Word.Range
Dim rngFound As Word.Range
Dim lngCharacters As Long
Dim lngCharactersWithSpaces As Long
Dim lngFarEastCharacters As Long
Dim lngLines As Long
Dim lngPages As Long
Dim lngParagraphs As Long
Dim lngWords As Long
' Get the totals for the document
With ActiveDocument
lngCharacters = .ComputeStatistics(wdStatisticCharacters)
lngCharactersWithSpaces =
..ComputeStatistics(wdStatisticCharactersWithSpaces)
lngFarEastCharacters = .ComputeStatistics(wdStatisticFarEastCharacters)
lngLines = .ComputeStatistics(wdStatisticLines)
lngPages = .ComputeStatistics(wdStatisticPages)
lngParagraphs = .ComputeStatistics(wdStatisticParagraphs)
lngWords = .ComputeStatistics(wdStatisticWords)
End With
' Subtract out the table statistics
For Each tblItem In ActiveDocument.Tables
With tblItem.Range
lngCharacters = lngCharacters -
..ComputeStatistics(wdStatisticCharacters)
lngCharactersWithSpaces = lngCharactersWithSpaces - _
..ComputeStatistics(wdStatisticCharactersWithSpaces)
lngFarEastCharacters = lngFarEastCharacters - _
..ComputeStatistics(wdStatisticFarEastCharacters)
lngLines = lngLines - .ComputeStatistics(wdStatisticLines)
lngPages = lngPages - .ComputeStatistics(wdStatisticPages)
lngParagraphs = lngParagraphs -
..ComputeStatistics(wdStatisticParagraphs)
lngWords = lngWords - .ComputeStatistics(wdStatisticWords)
End With
Next
' Search document for parenthesised words
Set rngReplace = ActiveDocument.Content
With rngReplace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\(<*>\)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
' Find all occurrences in the document
Do While .Execute
' Exclude statistics the words in parenthesis
Set rngFound = rngReplace.Duplicate
With rngFound
lngCharacters = lngCharacters -
..ComputeStatistics(wdStatisticCharacters)
lngCharactersWithSpaces = lngCharactersWithSpaces - _
..ComputeStatistics(wdStatisticCharactersWithSpaces)
lngFarEastCharacters = lngFarEastCharacters - _
..ComputeStatistics(wdStatisticFarEastCharacters)
lngWords = lngWords - .ComputeStatistics(wdStatisticWords)
End With
' Setup range to continue the search after
' the text that we just found
rngReplace.Collapse wdCollapseEnd
Loop
End With
MsgBox "Pages: " & lngPages & vbCr & _
"Paragraphs: " & lngParagraphs & vbCr & _
"Lines: " & lngLines & vbCr & _
"Words: " & lngWords & vbCr & _
"Characters: " & lngCharacters & vbCr & _
"Characters with spaces: " & lngCharactersWithSpaces & vbCr & _
"Far east Characters: " & lngFarEastCharacters
End Sub

Even then, you will need to watch out for line breaks that are inserted by
the email program.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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