Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Text in a word document

Status
Not open for further replies.

drluggo

Programmer
Jan 14, 2003
39
US
I want to search a word document for the occurence of a piece of text, ("EXPRESSION=") I then want to move a couple of characters to the right and selct the actual expression and completely modify it. The code to modfify the expression works fine. The problem I have is searching the entire document until the end. I have tried using the range and selection find objects and have had no luck.

Remember, I want to search for a string, move a little, make a change, then look for the next occurence. Do this until the entire document has been searched. Here is my code. I would appreciate any help:

Sub Main()

Dim strFoundText As String
Dim strBuiltString As String

Dim rngRange As Word.Range
Set rngRange = ThisDocument.Content

Selection.HomeKey Unit:=wdStory

With rngRange.Find
.ClearFormatting
.Text = "EXPRESSION="

While .Execute
Selection.MoveUntil "'"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveEndUntil "."

strFoundText = Selection.Text

If (InStr(strFoundText, "DR") And (Mid(strFoundText, 1, 1) = "7" Or Mid(strFoundText, 1, 1) = "8")) Then
strBuiltString = CreateNewString(strFoundText)
If (strBuiltString <> "NG") Then
Selection.Text = strBuiltString
End If
End If
Selection.Move Unit:=wdCharacter, Count:=1
Wend

End With

End Sub

Function CreateNewString(ByRef strWork As String) As String

Dim strNumber As String
Dim strSuffix As String
Dim strAttribute As String
Dim strInstType As String

Dim Result As VbMsgBoxResult

strNumber = Mid(strWork, 1, 2)

strSuffix = Mid(strWork, 3, 2)
Select Case strSuffix
Case "AD": strSuffix = "A"
Case "PG": strSuffix = "B"
Case "PR": strSuffix = "D"
Case "DP": strSuffix = "C"
End Select

strAttribute = Mid(strWork, 6, 2)
Select Case strAttribute
Case "MD":
strAttribute = "MODE"
strInstType = "XY"
Case "SP":
strAttribute = "SP"
strInstType = "XY"
Case "PV":
strAttribute = "PV"
strInstType = "PI"
End Select

CreateNewString = "//" & strInstType & "-" & strNumber & strSuffix & _
"/" & strAttribute

Result = MsgBox("Is " & strWork & " O.K.?", vbOKCancel)

If (Result <> vbOK) Then
CreateNewString = "NG"
End If

End Function

I realize that the range find method does not cahnge the selction, but when I use the same code witht the slection object, I cannnot get past the first occurrence.
 
Check the help files on the .FindNext method - that should get you where you need to go!


VBAjedi [swords]
 
I cannot find a FindNext method for the range or selection objects. I m using MS Word 2002
 
Oh - sorry. I mostly work in Excel, and I forgot Word doesn't use FindNext.

In Word, you just call the Execute method repeatedly. Put it in a "Do While. . ." loop along the lines of:
Code:
 Dim strFoundText As String
    Dim strBuiltString As String
    
    Dim rngRange As Word.Range
    Set rngRange = ThisDocument.Content

    Selection.HomeKey Unit:=wdStory
    
   With rngRange
      With .Find
         .ClearFormatting
         .Text = "EXPRESSION="
         .Replacement.Text = ""
         .Forward = True
         .Wrap = wdFindStop
         .Format = False
         .MatchCase = False
         .MatchWholeWord = False
         .MatchWildcards = False
         .MatchSoundsLike = False
         .MatchAllWordForms = False
      End With
      Do While .Find.Execute
            Selection.MoveUntil "'"
            Selection.MoveRight Unit:=wdCharacter, Count:=1
            Selection.MoveEndUntil "."
        
            strFoundText = Selection.Text
    
            If (InStr(strFoundText, "DR") And (Mid(strFoundText, 1, 1) = "7" Or Mid(strFoundText, 1, 1) = "8")) Then
                strBuiltString = CreateNewString(strFoundText)
                If (strBuiltString <> "NG") Then
                    Selection.Text = strBuiltString
                End If
            End If
            Selection.Move Unit:=wdCharacter, Count:=1
      Loop
    End With
Let me know if that does it for you!



VBAjedi [swords]
 
Hi drluggo,

As you seem to realise, if you use rngRange.Find, there are lots of other bits of code to change, but I've just done a quick mock-up using Selection.Find and it does work for me.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top