VBA code to select text and search for condition...

C

Carl Wilson

I usually include code that I've written so far for a project that I have
problems with but it isn't working the way I wrote it so I want to pick your
brains.
I have a log file from a system that is over 3k pages long (depends on the
date range I ask for!) I have written code that cleans all the misc. crap
that is not important to me, now I need to write a code that will select the
text from the beginning of the file, until a certain string of characters is
found, let's say "----" this is an indication that a single record in the
file is complete. (selecting the stop string would be okay too) Then I want
the code to check to see if it finds a condition in the selected text, lets
say a particular word like "Howdy". If the file finds that condition I want
it to copy that file to another file.
Because of the size of the file I was trying to write the code to do like I
said except that if the code wasn't found, delete the selected text, start
again. Then the only thing left in the log file would be the records I
wanted. Problem, was it started looping the first time it found the code
since I wrote it to start at the beginning of the file. So either way will
work if you can tell me how to tell the dumb code to start the last place it
stopped. Extracting to a different file would be better so I don't destroy
the orginal log file. As I said, the data I am searching through is pretty
much unpredictable except for the end record string, that's why I need to be
able to search the selected text for the condition.
This would be extremly helpful. Thanks.
Willy
 
C

Cindy M.

Hi Carl,
I usually include code that I've written so far for a project that I have
problems with but it isn't working the way I wrote it so I want to pick your
brains.
Mmmm, not sure I'm following 100%, but...

I'd work with multiple Range objects. That should give you the best control
over "what's where".

Say you start with a Range object the represents the entire document.

Then you want a Range object to do the first search. To begin with, this is the
same as the original range, so you use the Duplicate property to instantiate
it.

Then you should have a Range that extends from the start of the original range
to the end of the found range. You can create that using the Range method,
getting the Start and End properties of the other two ranges.

If I'm understanding correctly, it probably wouldn't be a bad idea to have a
fourth range for the second search, set to a duplicate of the third range.

Very roughly, the code would look something like this:

Dim rngDoc as Word.Range
Dim rngSearchCode as Word.Range
Dim rngRecord as Word.Range
Dim rngSearchHowdy as Word.Range

Set rngDoc = ActiveDocument.Content
Set rngSearchCode = rngDoc.Dupliate
With rngSearchCode.Find
'parameters here
.Wrap = wdFindStop 'Make sure it stays in the range!
bFound = Execute
If bFound Then
Set rngREcord = ActiveDocument.Range(rngDoc.Start, rngSearchCode.End)
Set rngSearchHowdy = rngRecord.Duplicate
With rngSearchHowdy.Find
'parameters here
If bFound Then
'Copy rngRecord to a new file
Else
rngRecord.Delete
End If
End with
End If
End With
I have a log file from a system that is over 3k pages long (depends on the
date range I ask for!) I have written code that cleans all the misc. crap
that is not important to me, now I need to write a code that will select the
text from the beginning of the file, until a certain string of characters is
found, let's say "----" this is an indication that a single record in the
file is complete. (selecting the stop string would be okay too) Then I want
the code to check to see if it finds a condition in the selected text, lets
say a particular word like "Howdy". If the file finds that condition I want
it to copy that file to another file.
Because of the size of the file I was trying to write the code to do like I
said except that if the code wasn't found, delete the selected text, start
again. Then the only thing left in the log file would be the records I
wanted. Problem, was it started looping the first time it found the code
since I wrote it to start at the beginning of the file. So either way will
work if you can tell me how to tell the dumb code to start the last place it
stopped. Extracting to a different file would be better so I don't destroy
the orginal log file. As I said, the data I am searching through is pretty
much unpredictable except for the end record string, that's why I need to be
able to search the selected text for the condition.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
H

Helmut Weber

Hi Carl,
over 3k pages long

is a lot, and I wonder, whether Word is the right tool
for your task. As I understand your posting, you want to
move a kind of window over your file, first from the start
of the doc until the search string, then from the end of
the found string to the next ocurrence of it,
and check, whether there is a certain string in it
and act accordingly.

I think that this is quite difficult and requires a deeper
understanding of how ranges work, however,
just one method of many:

Sub Test4008a()
Dim rDcm1 As Range
Set rDcm1 = ActiveDocument.Range
Dim rDcm2 As Range
Set rDcm2 = ActiveDocument.Range
Dim rDcm3 As Range
Set rDcm3 = Selection.Range

With rDcm1.Find
.Text = "----"
While .Execute
rDcm2.End = rDcm1.End
rDcm2.Select ' for testing remove later
Set rDcm3 = rDcm2.Duplicate
With rDcm2.Find
.Text = "Howdy"
If .Execute Then
MsgBox "howdy"
' copy to another file
Else
rDcm3.Delete
End If
End With
rDcm1.Collapse Direction:=wdCollapseEnd
rDcm1.End = ActiveDocument.Range.End
rDcm2.start = rDcm1.start
Wend
End With
End Sub


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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