Word 2003: Find all occurences macro...

A

Andrey Kazak

Greetings!

Could you help me please to compose a macro, which searches and selects ALL
matches to Edit -> Find criterion?

The action I want to simulate is as if one would select Find option from
Edit menu, enter search criterion, tick "Highlight all items found in" check
box, select "Main document" and push "Find All" button.

Thank you in advance for quick reply...
 
G

Graham Mayor

I'm pretty sure that isn't possible in vba. You would have to process each
individual items in a loop.

Something like

Dim oRng As Range
Dim sText As String
sText = "Text to find"
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=sText, _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set oRng = Selection.Range 'The found text
'Do what you want with oRng here e.g.
oRng.HighlightColorIndex = wdBrightGreen
Loop
End With
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Pesach Shelnitz

Hi,

I think that I found a way to do this in VBA without a loop. Try this.

Sub HighlightAllItems()
Dim myRange As Range
Dim searchText As String

searchText = "text to find"

Set myRange = ActiveDocument.Range
With myRange.Find
.Text = searchText
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Replacement.Text = searchText
.Forward = True
.Format = True
.Execute Replace:=wdReplaceAll
End With
End Sub
 
G

Graham Mayor

True that will emulate my sample macro without a loop, but sadly it doesn't
do what the OP asks. Although I used highlight as an (unfortunate) example,
what the user wanted was to *select* all the occurrences, which is what the
find dialog option does - and it is that which I don't believe can be
achieved with vba. The loop thus provides a much wider option to process the
found text.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

Andrey Kazak

This works fine but goes in infinite loop.
How can start processing from the very beginning of document and finish at
the end of the document, returning cursor to original (before Find) position?
 
A

Andrey Kazak

My main task is to apply center alignment to the all inline images
(inlineshapes) in the active document.
 
A

Andrey Kazak

Possible solution:

===
Sub CenterAllFigures()

Dim i As Integer

With Selection
.HomeKey wdStory, wdMove
With .Find
.ClearFormatting
.Replacement.ClearFormatting
For i = 1 To ActiveDocument.InlineShapes.Count
.Execute FindText:="^g^p", _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue, _
Format:=False
Selection.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next i
End With
End With

End Sub
===
Any comments?
 
G

Graham Mayor

You don't need anything so complicated

With ActiveDocument
For i = .InlineShapes.Count To 1 Step -1
.InlineShapes(i).Range.ParagraphFormat.Alignment _
= wdAlignParagraphCenter
Next i
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

Andrey Kazak

One thing left: how can I save original cursor position and restore it after
processing?
 
J

Jay Freedman

If you use Graham's code, and don't use Selection.Find or any other reference to
Selection anywhere in your macro, then the cursor position won't change at all,
and you don't need to restore it.

If you must use the Selection object in a way that moves it (and there are only
a few such circumstances that can't be done with a Range object instead), then
save and restore the position this way:

Dim myRange As Range
Set myRange = Selection.Range

' do work with Selection

myRange.Select

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
T

tolga

"Graham Mayor":
I'm pretty sure that isn't possible in vba. You would have to process each
individual items in a loop.

Something like

Dim oRng As Range
Dim sText As String
sText = "Text to find"
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=sText, _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set oRng = Selection.Range 'The found text
'Do what you want with oRng here e.g.
oRng.HighlightColorIndex = wdBrightGreen
Loop
End With
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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