G
Greg Maxey
Today I posted some code in the DocManagement group for deleteing duplicate
email entries from a document.
While working on this code I came across some behaviour with the .Find
method that I don't understand. Basically my approach was to search the
entire document for the first email address, set a variable to the end of
the first found item, look at the field code of each field in the document
and if it matched the first e-mail then delete the field, redefine the
search range to begin at the end of the first found e-mail and search for
the next e-mail address.
For some reason even though the search range didn't include the first e-mail
address it was still being found!!???
Here is the final code that worked. I marked where I had to add code to
overcome this behaviour. Any help in understanding this is appreciated.
Thanks.
Sub ScratchMacroII()
Dim pStr As String
Dim oRng As Range
Dim i As Long
Dim j As Long
Dim oFld As Field
Dim bLoop As Boolean
i = 1
bLoop = True
Do
Set oRng = ActiveDocument.Range
oRng.Start = i
With oRng.Find
.Text = "?{1,}\@?{1,}.?{3}"
.MatchWildcards = True
Do
.Execute
If Not .Found Then
bLoop = False
Exit Do
End If
If oRng.Start >= i Then 'Added this line to ignore a found item
preceeding the set search range
pStr = Trim(oRng.Text)
i = oRng.End
j = 0
For Each oFld In ActiveDocument.Fields
If InStr(oFld.Code, pStr) > 0 Then
j = j + 1
If j > 1 Then
oFld.Delete
End If
End If
Next oFld
Exit Do
Else ' Added this line
oRng.Collapse wdCollapseEnd 'Added this line
End If ' Added this line
Loop While .Found = True
End With
Loop While bLoop = True
End Sub
email entries from a document.
While working on this code I came across some behaviour with the .Find
method that I don't understand. Basically my approach was to search the
entire document for the first email address, set a variable to the end of
the first found item, look at the field code of each field in the document
and if it matched the first e-mail then delete the field, redefine the
search range to begin at the end of the first found e-mail and search for
the next e-mail address.
For some reason even though the search range didn't include the first e-mail
address it was still being found!!???
Here is the final code that worked. I marked where I had to add code to
overcome this behaviour. Any help in understanding this is appreciated.
Thanks.
Sub ScratchMacroII()
Dim pStr As String
Dim oRng As Range
Dim i As Long
Dim j As Long
Dim oFld As Field
Dim bLoop As Boolean
i = 1
bLoop = True
Do
Set oRng = ActiveDocument.Range
oRng.Start = i
With oRng.Find
.Text = "?{1,}\@?{1,}.?{3}"
.MatchWildcards = True
Do
.Execute
If Not .Found Then
bLoop = False
Exit Do
End If
If oRng.Start >= i Then 'Added this line to ignore a found item
preceeding the set search range
pStr = Trim(oRng.Text)
i = oRng.End
j = 0
For Each oFld In ActiveDocument.Fields
If InStr(oFld.Code, pStr) > 0 Then
j = j + 1
If j > 1 Then
oFld.Delete
End If
End If
Next oFld
Exit Do
Else ' Added this line
oRng.Collapse wdCollapseEnd 'Added this line
End If ' Added this line
Loop While .Found = True
End With
Loop While bLoop = True
End Sub