Att: Helmut 'Problems with search and extend '

D

Dave Neve

Hi Helmut,

Sorry to yet again have to ask but I just can't master VB.

You gave me two macros last week which I have copied below.

There are several problems which I just can't seem to get round.

1 The macros find words in all colums even though there is an instruction

Selection.SelectColumn

Something in the macro is overriding this instruction!

2 The instruction

.Replacement.Font.Underline = wdUnderlineSingle

is no good for me as I use 'underline' to indicate sth already (words
with low tones). Also, it is 'permanent' in that it doesn't disappear at the
end of a search. (if I make it disappear, all my underlined words are
changed which is a disaster)

I need the same behaviour as when Word searches. I have tried to see what
Word does by recording a macro and I think it is

.Selection. Extend

but I just can't get my macros to search for a Word , select and extend
every occurence

In summary, I would like to write a code to do the following which I can
already do with Word but it takes time

1 I select a word by clicking on it.

2 Macro searches for this word in a column (I tick various options before
running the search)

3 Macro 'highlights' every occurence of this word in column ('highlight
here' = extends and not 'highlight')

4 When I click on the page elsewhere, all the selected words return to
normal (no traces of search are left on the words found)

Thanks

PS I am wading through my second book on VBA: Création de Macros VBA by
Campus Press.

Thanks



Sub Dave()
Dim oCll As Cell
Dim sSrc As String
sSrc = Selection.Text
Resetsearch
Selection.SelectColumn
For Each oCll In Selection.Cells
With oCll.Range.Find
.Text = sSrc
.Replacement.Text = sSrc
.Replacement.Font.Underline = wdUnderlineSingle
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next
Resetsearch
End Sub
' ---
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
---
 
H

Helmut Weber

Hi Dave,
this is working here and now.
Sub Dave()
Dim oCll As Cell
Dim sSrc As String
sSrc = Selection.Text
Resetsearch
Selection.SelectColumn
For Each oCll In Selection.Cells
With oCll.Range.Find
.Text = sSrc
.Replacement.Text = sSrc
.Replacement.Font.Underline = wdUnderlineSingle
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next
Resetsearch
End Sub
Remember, I gave you a second macro, with "For Each oCll In
Selection.Cells" marked as comment, to demonstrate, that just to
select the column is of no use. That was to the best of my knowledge
yesterday. At that time, I had not discovered yet, what
".Wrap = wdFindStop" could achieve in a table. Seems that this is
the crucial point: So, here we go again:
Sub DaveHighlight()
Dim sSrc As String
Options.DefaultHighlightColorIndex = wdYellow
sSrc = Selection.Text
Resetsearch
Selection.SelectColumn
With Selection.find
.Text = sSrc
.Replacement.Text = sSrc
.Replacement.Highlight = True
.MatchWholeWord = False ' or true if you like
.Wrap = wdFindStop ' !!!
.Execute Replace:=wdReplaceAll
End With
Resetsearch
End Sub
---
Removing highlighting automatically after leaving a table,
would require a WindowSelectionChange-Event.
See: "Writing application event procedures"
http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm
Article contributed by Dave Rado, with acknowledgements to Bill Coan.
Heavy stuff for a beginner, but there is always a first time.
Was the first time for me, too, to really force replacements in
a column to perform, as I wanted.
HTH
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
D

Dave Neve

Hi Helmut

I really do feel guilty cos you always end up writing my macros.

I want to assure you that I do try and in fact wondered about Wrap =
wdFindStop

Problem was when I clicked on it and then ? in the contextual menu, there
was a message saying that there was no info on this command.

So I just had to leave it and now it turns out that it was crucial.

Dave
 
H

Helmut Weber

Hi Dave,
if I put the cursor in wrap and press F1, I get help
for "wrap-property". If you put the cursor in "wdFindStop",
you get no help. Press F2 and search for "wdfind".
Successfull help makes at least two people feel better,
and I love writing possibly so far unwritten macros.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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