Paste into find dialog using macros.

  • Thread starter Thread starter John
  • Start date Start date
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?
 
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

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
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.
 
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.
 
Back
Top