Extending a selection with a find

E

EllenM

I am trying to extend a selection with a find via the following script.



Sub TEST_FIND()
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub


I have the following text in my document in which my cursor is at the open
square bracket .
[ Name of Recipient ]

When the script executes, only the closing square bracket is highlighted.
Essentially, I am trying to use the “extend†method to extend a selection
from the cursor’s position up to the point in the document where the
character or word you want to find is found.

Oddly enough I can get the script above to work using the Normal.dot profile
that someone gave me off their machine but can not get it to run with my own
Normal.dot profile. This leads me to believe I need to change some settings
in Word to obtain the same extend functionality. Any suggestions besides
using my friend's Normal.dot file?

Thanks in advance for your help,
Ellen
 
L

Lene Fredborg

I cannot tell why you experience what you describe. However, you should be
able to extend the selection without the Find part of your code.

What happens if you replace all your code by the following code only - does
it work?

With Selection
.StartIsActive = False
.Extend Character:="]"
End With

Note that you may not need the StartIsActive line. However, if the selection
is not collapsed to an insertion point before running the code, setting
StartIsActive to false will make sure that the current selection is included
in the new selection.

From the VBA Help on the StartIsActive property:
True if the beginning of the selection is active. If the selection is not
collapsed to an insertion point, either the beginning or the end of the
selection is active. The active end of the selection moves when you call the
following methods: EndKey , Extend (with the Characters argument), HomeKey ,
MoveDown , MoveLeft , MoveRight , and MoveUp . Read/write Boolean.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
H

Helmut Weber

Hi Ellen,

something along these lines:

Sub Test4b()
ResetSearch
With Selection
.ExtendMode = True
.Find.Text = "]"
.Find.Execute
End With
End Sub
' -----------------------------
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

Helmut Weber

Hi Lene,

cool,

so to speak.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
L

Lene Fredborg

Hi Helmut,

Thanks - let’s see whether it works for Ellen.

Actually, I have never used Extend by specifying a character until I looked
at Ellen’s question. But I am sure I will use it again. That is one of the
great things about this forum - you always learn something new, not only when
you read answers from others but also when you try to answer a question
yourself ;-)

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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