J
jmmiller
I've created documents where every sentence is its own section and
want to count the number of times a given set of words/phrases appears
in each section of the given document. The following macro counts the
words/phrases and provides an output at the end of the document (to be
copied and pasted into excel where I will be looking at combinations
of the words and phrases).
I would like to have a larger set of search words/phrases (15-25) and
do this on 200 documents of 100+ pages each. Currently, it takes hours
for the macro to work on a given document and I think this is because
it of how it is looping through the sections, but am not sure.
Is there a better way to be doing this? Any suggestions or ideas? Is
there a better way to do this where it would search between periods
rather than using section breaks to make it faster or another way for
it to search through the document by sentence?
Any help is much appreciated.
Sub JMS_WordCount()
Application.Windows(ActiveDocument).View = wdNormalView
Application.ScreenUpdating = False
Application.Options.Pagination = False
Application.ActiveDocument.ShowGrammaticalErrors = False
Application.ActiveDocument.ShowSpellingErrors = False
Dim CurrPane As Pane
Set CurrPane = Application.Documents(1).Windows(1).ActivePane
Dim myRange As Range
Dim myWord As Range
Dim i As Long
Dim Unemploy As Long
Dim Underemp As Long
Dim Inflation As Long
'Selects the active document, collapse to the end,
'and puts a final section break at the end
With ActiveDocument.Range
.Collapse wdCollapseEnd
.InsertBreak Type:=wdSectionBreakContinuous
End With
'Identifies total number of sections as Total minus 1
For i = 1 To ActiveDocument.Sections.Count - 1
''''''''''''''''''''''''' UMEMPLOY
Set myRange = ActiveDocument.Sections(i).Range
Unemploy = 0
Selection.HomeKey wdStory 'Story refers to all that's in a
distinct unit
'HomeKey refers to the start of the
specified unit
'Here, that unit is Section(i)
Selection.Find.ClearFormatting
With Selection.Find 'Selection here is Section(i)
Do While .Execute(FindText:="<[Uu]nemploy",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set myWord = Selection.Range 'Select the word
range when found
If myWord.InRange(myRange) = True Then 'If
selected word is in search range, then
Unemploy = Unemploy + 1 'count it
End If
Loop
End With
'''''''''''''''''''''''' UNDEREMPLOY
Set myRange = ActiveDocument.Sections(i).Range
Underemp = 0
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="<([Uu]nderemploy)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set myWord = Selection.Range
If myWord.InRange(myRange) = True Then
Underemp = Underemp + 1
End If
Loop
End With
'''''''''''''''''''''''' INFLATION
Set myRange = ActiveDocument.Sections(i).Range
Inflation = 0
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="([Ii]nflation)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set myWord = Selection.Range
If myWord.InRange(myRange) = True Then
Inflation = Inflation + 1
End If
Loop
End With
''''''''''''''''''''''''''
ActiveDocument.Range.InsertAfter Unemploy & vbTab & Underemp &
vbTab & Inflation & vbCr
Next i
End Sub
want to count the number of times a given set of words/phrases appears
in each section of the given document. The following macro counts the
words/phrases and provides an output at the end of the document (to be
copied and pasted into excel where I will be looking at combinations
of the words and phrases).
I would like to have a larger set of search words/phrases (15-25) and
do this on 200 documents of 100+ pages each. Currently, it takes hours
for the macro to work on a given document and I think this is because
it of how it is looping through the sections, but am not sure.
Is there a better way to be doing this? Any suggestions or ideas? Is
there a better way to do this where it would search between periods
rather than using section breaks to make it faster or another way for
it to search through the document by sentence?
Any help is much appreciated.
Sub JMS_WordCount()
Application.Windows(ActiveDocument).View = wdNormalView
Application.ScreenUpdating = False
Application.Options.Pagination = False
Application.ActiveDocument.ShowGrammaticalErrors = False
Application.ActiveDocument.ShowSpellingErrors = False
Dim CurrPane As Pane
Set CurrPane = Application.Documents(1).Windows(1).ActivePane
Dim myRange As Range
Dim myWord As Range
Dim i As Long
Dim Unemploy As Long
Dim Underemp As Long
Dim Inflation As Long
'Selects the active document, collapse to the end,
'and puts a final section break at the end
With ActiveDocument.Range
.Collapse wdCollapseEnd
.InsertBreak Type:=wdSectionBreakContinuous
End With
'Identifies total number of sections as Total minus 1
For i = 1 To ActiveDocument.Sections.Count - 1
''''''''''''''''''''''''' UMEMPLOY
Set myRange = ActiveDocument.Sections(i).Range
Unemploy = 0
Selection.HomeKey wdStory 'Story refers to all that's in a
distinct unit
'HomeKey refers to the start of the
specified unit
'Here, that unit is Section(i)
Selection.Find.ClearFormatting
With Selection.Find 'Selection here is Section(i)
Do While .Execute(FindText:="<[Uu]nemploy",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set myWord = Selection.Range 'Select the word
range when found
If myWord.InRange(myRange) = True Then 'If
selected word is in search range, then
Unemploy = Unemploy + 1 'count it
End If
Loop
End With
'''''''''''''''''''''''' UNDEREMPLOY
Set myRange = ActiveDocument.Sections(i).Range
Underemp = 0
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="<([Uu]nderemploy)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set myWord = Selection.Range
If myWord.InRange(myRange) = True Then
Underemp = Underemp + 1
End If
Loop
End With
'''''''''''''''''''''''' INFLATION
Set myRange = ActiveDocument.Sections(i).Range
Inflation = 0
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="([Ii]nflation)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set myWord = Selection.Range
If myWord.InRange(myRange) = True Then
Inflation = Inflation + 1
End If
Loop
End With
''''''''''''''''''''''''''
ActiveDocument.Range.InsertAfter Unemploy & vbTab & Underemp &
vbTab & Inflation & vbCr
Next i
End Sub