PDA

View Full Version : String search



norgro
08-16-2012, 08:54 PM
I want to highlight the string "ment" when it is not at the beginning of a word.

The code works except when the string is at the beginning of the document – e.g. when the first word in the document is "Mention". No previous character.

Your help would be appreciated.

Set oRng = ActiveDocument.Range
With oRng.Find
.MatchWholeWord = False
.Text = "ment"
.Wrap = wdFindStop 'stops at the end of the document
While .Execute
MsgBox ((Asc(oRng.Characters.First.Previous)))
If ((Asc(oRng.Characters.First.Previous) > 65 And Asc(oRng.Characters.First.Previous) < 90) _
Or (Asc(oRng.Characters.First.Previous) > 97 And Asc(oRng.Characters.First.Previous) < 122)) _
...

gmaxey
08-16-2012, 09:40 PM
Is the rest of your code a state secret? It is a lot easier and less time consuming if we don't have to make your code complete before testing!!

Causes an error is more accurate than "works except." Errors can be handled:

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRNg As Word.Range
Set oRNg = ActiveDocument.Range
With oRNg.Find
.MatchWholeWord = False
.Text = "ment"
.Wrap = wdFindStop 'stops at the end of the document
While .Execute
O n Error GoTo Err_Handler
If ((Asc(oRNg.Characters.First.Previous) > 65 And Asc(oRNg.Characters.First.Previous) < 90) _
Or (Asc(oRNg.Characters.First.Previous) > 97 And Asc(oRNg.Characters.First.Previous) < 122)) Then
'What??
End If
Err_ReEntry:
Wend
End With
Exit Sub
Err_Handler:
Resume Err_ReEntry:
End Sub


However, you are only interested in instances of "ment" appearing after the start of the word, so don't evaluate "Mention."

Sub ScratchMacroII()
'A quick macro scratch pad created by Greg Maxey
Dim oRNg As Word.Range
Set oRNg = ActiveDocument.Range
With oRNg.Find
.MatchWholeWord = False
.Text = "ment"
.Wrap = wdFindStop 'stops at the end of the document
While .Execute
If oRNg.Characters.First <> oRNg.Words(1).Characters.First Then
If ((Asc(oRNg.Characters.First.Previous) > 65 And Asc(oRNg.Characters.First.Previous) < 90) _
Or (Asc(oRNg.Characters.First.Previous) > 97 And Asc(oRNg.Characters.First.Previous) < 122)) Then
'What??
End If
End If
Wend
End With
Exit Sub
End Sub

macropod
08-17-2012, 01:49 AM
Try:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Content
With .Find
.ClearFormatting
.Text = "ment"
With .Replacement
.ClearFormatting
.Text = ""
End With
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
.Execute
End With
While .Find.Found
Set Rng = .Duplicate.Words.First
While Rng.Characters.Last = " "
Rng.End = Rng.End - 1
Wend
With .Duplicate
If .Start <> Rng.Start And .End <> Rng.End Then
.HighlightColorIndex = wdYellow
End If
End With
.Collapse wdCollapseEnd
.Find.Execute
Wend
End With
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
The above assumes neither end of a word.

norgro
08-17-2012, 02:47 AM
Thanks Paul.
I really appreciate your help.
Norman

norgro
08-17-2012, 02:56 AM
Thanks Greg. Sorry about not including the full code. This is still a learning experience for me. Your help is really great, and I really appreciate what you do for this forum. Norman