please explain

N

n6trf

I have the following macro which i use to check for duplicate entries in a
document. what i do not understand is, when I remove the second 'with
selection.find ... end with' then the 3rd time I execute
selection.find.execute I get no movement of the cursor. I assumed the 'with
selection.find ... end with' would apply to all the selection.find.executes.
Help please


With Selection.Find
' .Text = Selection()
.Text = strTem
.Replacement.Text = ""
.Forward = True
' .Wrap = wdFindAsk
' .Wrap = wdFindStop
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If vbCancel = MsgBox("Thats once", vbOKCancel) Then Exit Sub
Selection.Find.Execute
If vbCancel = MsgBox("Thats twice: We should NOT be in the list again",
vbOKCancel) Then Exit Sub

' Selection.MoveRight Unit:=wdCharacter, Count:=2
With Selection.Find
' .Text = Selection()
.Text = strTem
.Replacement.Text = ""
.Forward = True
' .Wrap = wdFindAsk
' .Wrap = wdFindStop
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If vbCancel = MsgBox("Thats thrice: We should BE in the list again",
vbOKCancel) Then Exit Sub
 
T

Tony Jollans

Find will 'fail' (return False, meaning not found) once all instances of the
find text have been found. Setting Wrap to Continue simply means it will go
from the end of the document back to the beginning but it will still finish
once the whole document has been searched (Wrap = Stop will end at the end
of the document whether or not the whole document has been searched and Wrap
= Ask will prompt for whether to Stop or Continue). When the cursor hasn't
moved it is because the .Execute has returned False.

The second With ... End With block forces a start of another Find operation,
which will search the whole document (again) from wherever the start happens
to be at that point.
 
N

n6trf

There must be some tutorial, book ... that explains all this. I have a
'teach yourself visual basic' but it never gets into access or word
specifics. Got any ideas? TIA
 
T

Tony Jollans

This isn't a VBA issue - VBA is just reflecting Word's behaviour.

The trouble with finding a suitable book is that 'all this' is a huge amount
probably not all covered in any one book; I don't have any books and don't
know of any that might cover this kind of thing - I'll have to write one :)
 

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