J
jmmiller
I've created documents where every sentence is its own section. I
want/need to create a table/matrix/some sort of output at the end of
each document that will indicate whether or not each of a list of
words appears in a given section. I want it to output a 1 if the find
function is successful and a 0 if it is not (so it will look like a
matrix/table with each section being a row and each column
representing the presence of a search term).
I want to be able to export the data into excel (to look for
combinations/other analysis/etc.) so any sort of output (separated by
tabs/in a table/anything) would work. I've been trying to get a macro
to do this using a matrix/array to record the data and then just write
it out at the end (I've been trying to get it in a table, listbox,
combobox, etc) but can't seem to get it to work. Macro 1 pasted below
is a short version of as far as I have been able to get.
I do have a macro that gives me the output I am looking for, but
takes an extremely long time (some documents are 400,000 words or so;
there are dozens of documents) because it goes back and forth between
the section it is examining and the end of the document (I will paste
that macro below as macro 2) to write out one line of output at a time
before moving on to the next section. I'm pretty sure the array/matrix
method would be much quicker as it seems most of the time is taken up
going back and forth between a section and the output, but don't know
how to do it.
How do I get the array inserted into the document? Or is there a
better way of doing this? Any suggestions/ideas would be great.
I appreciate any help.
Macro1(does not work, but might be a start; I left my notes/attempts
at a table/listbox in):
Sub Matrix1()
'
' Macro1 Macro
' Macro recorded 5/1/2009 by WITS
'
Dim myRange As Range
Dim IsPresent As Integer
Dim myArray() As String
Dim mySection As Integer
‘Dim ListBox1 As ListBox
Dim myNumWords As Integer
Dim myTotSect As Integer
Dim rtable As Table
myNumWords = 3 ' number of words to search for
myTotSect = ActiveDocument.Sections.Count
ReDim myArray(1 To myTotSect, 1 To myNumWords) As String
For mySection = 1 To myTotSect
Set myRange = ActiveDocument.Sections(mySection).Range
'ListBox1.ColumnCount = myNumWords
'Rows = myTotSect
' You'll have one If statement like the following for each search
word
' This code stores a 1 in the appropriate array entry if the word
is
' found
myRange.Find.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Uu]nemploy)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) Then
myArray(mySection, 1) = 1
Else
myArray(mySection, 1) = 0
End If
myRange.Find.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Uu]nderemploy)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) Then
myArray(mySection, 2) = 1
Else
myArray(mySection, 2) = 0
End If
myRange.Find.ClearFormatting
If myRange.Find.Execute(Findtext:="salad", Wrap:=wdFindContinue)
Then
myArray(mySection, 3) = 1
Else
myArray(mySection, 3) = 0
End If
Next mySection
'Set rtable = .Tables.Add(Selection.Range, myTotSect, 9)
' With rtable
' For x = 1 to myTotSect
' For y = 1 to 9
' .Cell(x, y).Range.InsertAfter = (myArrary(x,y))
'End With
'ListBox1.List() = myArray()
'ActiveDocument.Range.InsertAfter ListBox1
End Sub
-------------------------------------------------------------------------------------------------
Macro2 (this works and gives me the output I want, but takes much too
long for the length and number of documents I am dealing with):
Sub SentenceWordYesNo()
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 Inflation As Long
Dim Fullemp As Long
Dim Nairu As Long
Dim Partrate As Long
Dim Labor As Long
Dim Wage As Long
Dim Vacrate As Long
Dim Price 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
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Uu]nemploy)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Unemploy
= 1 Else Unemploy = 0
End With
''''''''''''''''''''''''' Inflation
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:=" <([Ii]nflation)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Inflation
= 1 Else Inflation = 0
End With
''''''''''''''''''''''''' Employment (Fullemp)
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:=" <([Ee]mployment)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Fullemp =
1 Else Fullemp = 0
End With
''''''''''''''''''''''''' Nairu
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<(NAIRU)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Nairu = 1
Else Nairu = 0
End With
''''''''''''''''''''''''' Participation Rate
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Pp]articipation
rate)", MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Partrate
= 1 Else Partrate = 0
End With
''''''''''''''''''''''''' Labor
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Ll]abor[!ie])",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Labor = 1
Else Labor = 0
End With
''''''''''''''''''''''''' Wage
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Ww]age[!r])",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Wage =1
Else Wage = 0
End With
''''''''''''''''''''''''' Vacrate
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Vv]acancy
rate)", MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Vacrate =
1 Else Vacrate = 0
End With
''''''''''''''''''''''''' Price
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:=" <([Pp]rice[!d])",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Price = 1
Else Price = 0
End With
''''''''''''''''''''''''''
ActiveDocument.Range.InsertAfter Unemploy & vbTab & Inflation
& vbTab & Fullemp & vbTab & Nairu & vbTab & Partrate & vbTab & Labor &
vbTab & Wage & vbTab & Vacrate & vbTab & Price & vbCr
Next i
End Sub
want/need to create a table/matrix/some sort of output at the end of
each document that will indicate whether or not each of a list of
words appears in a given section. I want it to output a 1 if the find
function is successful and a 0 if it is not (so it will look like a
matrix/table with each section being a row and each column
representing the presence of a search term).
I want to be able to export the data into excel (to look for
combinations/other analysis/etc.) so any sort of output (separated by
tabs/in a table/anything) would work. I've been trying to get a macro
to do this using a matrix/array to record the data and then just write
it out at the end (I've been trying to get it in a table, listbox,
combobox, etc) but can't seem to get it to work. Macro 1 pasted below
is a short version of as far as I have been able to get.
I do have a macro that gives me the output I am looking for, but
takes an extremely long time (some documents are 400,000 words or so;
there are dozens of documents) because it goes back and forth between
the section it is examining and the end of the document (I will paste
that macro below as macro 2) to write out one line of output at a time
before moving on to the next section. I'm pretty sure the array/matrix
method would be much quicker as it seems most of the time is taken up
going back and forth between a section and the output, but don't know
how to do it.
How do I get the array inserted into the document? Or is there a
better way of doing this? Any suggestions/ideas would be great.
I appreciate any help.
Macro1(does not work, but might be a start; I left my notes/attempts
at a table/listbox in):
Sub Matrix1()
'
' Macro1 Macro
' Macro recorded 5/1/2009 by WITS
'
Dim myRange As Range
Dim IsPresent As Integer
Dim myArray() As String
Dim mySection As Integer
‘Dim ListBox1 As ListBox
Dim myNumWords As Integer
Dim myTotSect As Integer
Dim rtable As Table
myNumWords = 3 ' number of words to search for
myTotSect = ActiveDocument.Sections.Count
ReDim myArray(1 To myTotSect, 1 To myNumWords) As String
For mySection = 1 To myTotSect
Set myRange = ActiveDocument.Sections(mySection).Range
'ListBox1.ColumnCount = myNumWords
'Rows = myTotSect
' You'll have one If statement like the following for each search
word
' This code stores a 1 in the appropriate array entry if the word
is
' found
myRange.Find.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Uu]nemploy)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) Then
myArray(mySection, 1) = 1
Else
myArray(mySection, 1) = 0
End If
myRange.Find.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Uu]nderemploy)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) Then
myArray(mySection, 2) = 1
Else
myArray(mySection, 2) = 0
End If
myRange.Find.ClearFormatting
If myRange.Find.Execute(Findtext:="salad", Wrap:=wdFindContinue)
Then
myArray(mySection, 3) = 1
Else
myArray(mySection, 3) = 0
End If
Next mySection
'Set rtable = .Tables.Add(Selection.Range, myTotSect, 9)
' With rtable
' For x = 1 to myTotSect
' For y = 1 to 9
' .Cell(x, y).Range.InsertAfter = (myArrary(x,y))
'End With
'ListBox1.List() = myArray()
'ActiveDocument.Range.InsertAfter ListBox1
End Sub
-------------------------------------------------------------------------------------------------
Macro2 (this works and gives me the output I want, but takes much too
long for the length and number of documents I am dealing with):
Sub SentenceWordYesNo()
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 Inflation As Long
Dim Fullemp As Long
Dim Nairu As Long
Dim Partrate As Long
Dim Labor As Long
Dim Wage As Long
Dim Vacrate As Long
Dim Price 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
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Uu]nemploy)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Unemploy
= 1 Else Unemploy = 0
End With
''''''''''''''''''''''''' Inflation
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:=" <([Ii]nflation)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Inflation
= 1 Else Inflation = 0
End With
''''''''''''''''''''''''' Employment (Fullemp)
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:=" <([Ee]mployment)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Fullemp =
1 Else Fullemp = 0
End With
''''''''''''''''''''''''' Nairu
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<(NAIRU)",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Nairu = 1
Else Nairu = 0
End With
''''''''''''''''''''''''' Participation Rate
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Pp]articipation
rate)", MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Partrate
= 1 Else Partrate = 0
End With
''''''''''''''''''''''''' Labor
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Ll]abor[!ie])",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Labor = 1
Else Labor = 0
End With
''''''''''''''''''''''''' Wage
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Ww]age[!r])",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Wage =1
Else Wage = 0
End With
''''''''''''''''''''''''' Vacrate
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:="<([Vv]acancy
rate)", MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Vacrate =
1 Else Vacrate = 0
End With
''''''''''''''''''''''''' Price
Set myRange = ActiveDocument.Sections(i).Range
Selection.HomeKey wdStory
With Application.Selection.Find
.ClearFormatting
If myRange.Find.Execute(Findtext:=" <([Pp]rice[!d])",
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True Then Price = 1
Else Price = 0
End With
''''''''''''''''''''''''''
ActiveDocument.Range.InsertAfter Unemploy & vbTab & Inflation
& vbTab & Fullemp & vbTab & Nairu & vbTab & Partrate & vbTab & Labor &
vbTab & Wage & vbTab & Vacrate & vbTab & Price & vbCr
Next i
End Sub