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.
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.