K
Ken McLennan
G'day there One & All,
I'm currently working on my first Word VB project (well, the first
one I've not abandoned and forgotten about) and I've obviously got
something wrong.
I have some experience with VB for Excel, so most of the concepts
ring a bell or two, but the Word object model escapes me and I'm still
trying to get my head around it.
I have 3 years worth of occurrence logs that I need to search for
a specific string. The logs consist of a single table with some
consistent entries. Unfortunately this string appears in all logs in one
of those entries which I describe as a "heading area". I need to find it
in the "narrative" section, not one of the heading areas. The logs are
organised in Year Folders, with Month folders inside. The log names are
the date for each daily log.
I'm trying to perform the following tasks:
1. Use dialog to obtain enclosing folder for all documents - done.
2. Iterate through all subfolders to find log documents - done.
3. Search through each log in turn and search for one of the consistant
headings, then commence searching from the next line down for the target
word. If found, then store filename in base docuement and ...
4. Stop at end of document, and got to next one - done.
Courtesy of the Word MVP pages, and this group's archives, I think
I've got 1, 2 & 4 working OK.
However 3 is giving me a few problems. I'm getting false hits, and
missing occurrences of the target. Ergo, I've got something out of
whack. I hope that someone here may be able to point me in the right
direction.
Here's my searching code:
Application.Documents.Open FileName:=strPATH & strFILENAME (Do I need to
"open"?)
Application.Documents(strFILENAME).Activate
With Selection.Find
.Text = "Objectives C" ' Heading where search begins from next
line
.MatchWholeWord = True
.Font.Underline = wdUnderlineSingle
Do While .Execute
Selection.GoTo what:=wdGoToLine, which:=wdGoToNext
With Selection.Find
.Text = "hotel"
.Forward = True
.Wrap = wdFindStop
.MatchCase = False
.MatchWholeWord = True
If .Found = True Then
strFULLNAME = Mid(strPATH & strFILENAME, 78, Len(strPATH
& strFILENAME) - 77) ' Leave just parent folder and filename
If Len(objSTOREDOC.Content) = 0 Then _
objSTARTRANGE.InsertAfter strFULLNAME & vbCr
Else
objSTOREDOC.Content.Select
Selection.Collapse direction:=wdCollapseEnd
Selection.InsertAfter strFULLNAME & vbCr
End If
End If
End With
Loop
End With
Application.Documents(strFILENAME).Close
strFILENAME = Dir
(This is repeated in a loop)
Hopefully this will make sense and someone can point out a better way to
do it.
Thanks for listening,
I'm currently working on my first Word VB project (well, the first
one I've not abandoned and forgotten about) and I've obviously got
something wrong.
I have some experience with VB for Excel, so most of the concepts
ring a bell or two, but the Word object model escapes me and I'm still
trying to get my head around it.
I have 3 years worth of occurrence logs that I need to search for
a specific string. The logs consist of a single table with some
consistent entries. Unfortunately this string appears in all logs in one
of those entries which I describe as a "heading area". I need to find it
in the "narrative" section, not one of the heading areas. The logs are
organised in Year Folders, with Month folders inside. The log names are
the date for each daily log.
I'm trying to perform the following tasks:
1. Use dialog to obtain enclosing folder for all documents - done.
2. Iterate through all subfolders to find log documents - done.
3. Search through each log in turn and search for one of the consistant
headings, then commence searching from the next line down for the target
word. If found, then store filename in base docuement and ...
4. Stop at end of document, and got to next one - done.
Courtesy of the Word MVP pages, and this group's archives, I think
I've got 1, 2 & 4 working OK.
However 3 is giving me a few problems. I'm getting false hits, and
missing occurrences of the target. Ergo, I've got something out of
whack. I hope that someone here may be able to point me in the right
direction.
Here's my searching code:
Application.Documents.Open FileName:=strPATH & strFILENAME (Do I need to
"open"?)
Application.Documents(strFILENAME).Activate
With Selection.Find
.Text = "Objectives C" ' Heading where search begins from next
line
.MatchWholeWord = True
.Font.Underline = wdUnderlineSingle
Do While .Execute
Selection.GoTo what:=wdGoToLine, which:=wdGoToNext
With Selection.Find
.Text = "hotel"
.Forward = True
.Wrap = wdFindStop
.MatchCase = False
.MatchWholeWord = True
If .Found = True Then
strFULLNAME = Mid(strPATH & strFILENAME, 78, Len(strPATH
& strFILENAME) - 77) ' Leave just parent folder and filename
If Len(objSTOREDOC.Content) = 0 Then _
objSTARTRANGE.InsertAfter strFULLNAME & vbCr
Else
objSTOREDOC.Content.Select
Selection.Collapse direction:=wdCollapseEnd
Selection.InsertAfter strFULLNAME & vbCr
End If
End If
End With
Loop
End With
Application.Documents(strFILENAME).Close
strFILENAME = Dir
(This is repeated in a loop)
Hopefully this will make sense and someone can point out a better way to
do it.
Thanks for listening,