Paste into find dialog using macros.

J

John

In MS Word 2003 when recording macros and making search using Find and
Replace dialog window it alway's remembers text you use, how can i modify
macros to paste text from clipboard into Find what: field?
 
G

Graham Mayor

The macro recorder is limited in its abilities. To insert text from the
clipboard you would have to manually configure the macro to use text from
the clipboard e.g.

Dim sFindText As String
Dim sReplaceText As String
Dim oClip As DataObject
Set oClip = New DataObject
oClip.GetFromClipboard
sFindText = oClip.GetText
sReplaceText = "The text to replace the found text with"
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = sFindText
.Replacement.Text = sReplaceText
.Forward = True
.Wrap = wdFindContinue
.format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
End With


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

John

Hi, thank you for replay. I tried create macro then remove all code from it
except Sub and End Sub then paste code you posted below and i getting error
when running that macro in Basic editor it highlights this line "oClip As
DataObject". I use Office 2003 on Windows XP SP3.
 
J

Jay Freedman

In the VBA editor, click Tools > References. Find "Microsoft Forms 2.0
Object Library" in the list of available references and click to put a
check mark in its box. Click OK. The macro will now run.

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

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