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
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