Macro recorder not recording properly

A

Al Almoian

Word 2003. I do a wildcard Find Highlight all then copy, ctrlHome,paste,
it works great. But when I try to macro record this sequence it does not
work. The debug on the Copy statement says nothing is selected. And the
macro only has Find, not Find All. How do I fix this?
 
S

Stefan Blom

I'm afraid it can't be fixed. The macro recorder has limited capabilities. At
best, you can use it to get an understanding of which objects you need to look
in to when you want to perform a certain task in VBA. Recorded macros must
always (or almost always!) be edited if they should be truly useful.
 
G

Graham Mayor

The macro recorder, as others have suggested, cannot cope with this type of
action. You would need to create a roll-your-own function that would process
each found item separately. It seems that you want to insert all the found
items at the start of your document. The following macro will do that, with
each item separated by a comma and a space:

Dim oRng As Range
Dim sText As String
Dim sFound As String
sFound = ""
sText = InputBox("Enter the string to process")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:=sText, _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set oRng = Selection.Range
sFound = sFound & oRng.Text & ", "
Loop
End With
If Len(sFound) > 0 Then
sFound = Left(sFound, Len(sFound) - 2)
ActiveDocument.Range.InsertBefore sFound & vbCr
Else
MsgBox "Item not found", vbInformation, "Find text"
End If

http://www.gmayor.com/installing_macro.htm
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
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