Finding Text

T

Thomas M

Word 2000

I have the need for a macro that can find two text strings, and will
always find whichever one comes next. Basically, my data looks like
this:

Blah blah blah
Blah blah blah
Blah blah blah
MIN/W303956
Blah blah blah
Blah blah blah
Blah blah blah

--------------

Blah blah blah
Blah blah blah
Blah blah blah
Blah blah blah
NIC/W874265139
Blah blah blah
Blah blah blah

--------------

Blah blah blah
MIN/W545186
Blah blah blah
Blah blah blah
Blah blah blah

--------------

The strings I need to find are "MIN/" and "NIC/". These are fields from
a database, and the slash separates the field name from the data. I have
to find these fields in order--I can't just find all the MIN/ fields and
then all the NIC/ fields. So the macro would need to find the first
MIN/, then find the NIC/, then find the second MIN/.

Is there a way to have Word search for two text values at the same time,
and highlight whichever one it finds first? If not, what would be the
best way of going about this task?

I had the idea that maybe I could find all the MIN fields first, and
write the cursor location of each to an array, then find all the NIC
fields and write those cursor locations to the same array, then sort the
array by cursor location so that Word to go to each one in turn.
However, I have no idea how to do that! :)

--Tom
 
M

Malcolm Smith

If you worked with the Instr() function then you could easily do this.

You basically want a list of what appears at which position in which
order? Sounds easy enough.

Let me look at the cards for the Arc tomorrow then I'll see what I can
knock up...

- Malc
www.dragondrop.com
 
M

Malcolm Smith

Thomas

This code will find ANY number of strings within the text. You will need
to make a class module called 'clsNode' and this has the following code
(which is VERY small):



Option Explicit

Public nLocation As Long
Public sString As String





Then in the main module you have the following code. Note that the list
of strings you want to find are in the
'Array("Dog", "Lazy", "Bliar", "Fox", "Walnut")' line. You just update
this to add and subtract anything you want or don't want.

You execute the Main() procedure and then it creates a collection of Nodes
(the clsNode class) objects which lists, in order, the position of the
string and the string name.

Note that instances of non-existing search strings doesn't effect
anything.

I did a test document by having lots of "The Quick Brown Fox..." and it
worked fine for me.

If you have any questions then please do shout.

Regards
Malc
www.dragondrop.com




Option Explicit

Dim moNodes As Collection
Dim moNode As clsNode
'


Public Sub Main()

Dim avArray() As Variant

If Documents.Count > 0 Then

Set moNodes = New Collection

avArray() = Array("Dog", "Lazy", "Bliar", "Fox", "Walnut")
FindStrings ActiveDocument, avArray()
End If

End Sub



Private Sub FindStrings(oDoc As Document, asSearchStrings() As Variant)

Dim sText As String
Dim nNextPosition As Long
Dim nStart As Long


sText = oDoc.Range.Text
nStart = 1
nNextPosition = GetNextString(nStart, sText, asSearchStrings())

Do While nNextPosition > 0
nStart = nNextPosition + 1
nNextPosition = GetNextString(nStart, sText, asSearchStrings())
Loop

End Sub



Private Function GetNextString(nStartPosition As Long, sText As String,
asSearchStrings() As Variant) As Long

Dim nKey As Long
Dim sKey As String
Dim nFoundPosition As Long
Dim nLowestFoundPosition As Long
Dim sFoundText As String


nLowestFoundPosition = Len(sText) + 1

For nKey = 0 To UBound(asSearchStrings())
sKey = asSearchStrings(nKey)
nFoundPosition = InStr(nStartPosition, sText, sKey, vbTextCompare)
If nFoundPosition > 0 And nFoundPosition < nLowestFoundPosition Then
nLowestFoundPosition = nFoundPosition
sFoundText = sKey
End If
Next nKey


If nLowestFoundPosition > Len(sText) Then
nLowestFoundPosition = 0
Else
' Add the found information to the collection
Set moNode = New clsNode
moNode.nLocation = nLowestFoundPosition
moNode.sString = sFoundText
moNodes.Add moNode
End If


GetNextString = nLowestFoundPosition

End Function
 

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