How do I populate the "Find" dialogue in Word with the contents ofthe clipboard?

J

John

Hi

I tried, and failed to create a macro.

(I do a procedure hundreds of times every day so wanted to automate
it)

I press Control-F (to open the 'find' dialogue) and then press Control-
V (to paste the contents) into the dialogue. As you can see from the
code below, when I recorded the macro, it used '2022' (which is what
happens to be in my clipboard right now) but I want it to use the
clipboard.


Is this possible? I hope I've explained the problem properly.




====================================================





Sub FindWhatsinClipboard()
'
' FindWhatsinClipboard Macro
' Macro recorded 26/03/2010 by John Smith
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "2202"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "2202"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub
 
J

Jay Freedman

Hi John,

Replace the line

.Text = "2202"

with

.Text = Selection.Text

Also, everything the recorder put into the macro after the line

Selection.Find.Execute

is worthless and can be deleted. For enlightenment, see
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

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

John

Hi John,

Replace the line

     .Text = "2202"

with

      .Text = Selection.Text

Also, everything the recorder put into the macro after the line

    Selection.Find.Execute

is worthless and can be deleted. For enlightenment, seehttp://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso
all may benefit.








- Show quoted text -

Thanks very much
 
J

John

Hi John,

Replace the line

     .Text = "2202"

with

      .Text = Selection.Text

Also, everything the recorder put into the macro after the line

    Selection.Find.Execute

is worthless and can be deleted. For enlightenment, seehttp://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.


To Jay/other clever people,

Okay, a TWIST.

How can I get it to switch to a DIFFERENT Word document, to search for
the specific text (that was in the first Word document)

Thanks very much!
 
G

Graham Mayor

John said:
To Jay/other clever people,
Okay, a TWIST.
How can I get it to switch to a DIFFERENT Word document, to search for
the specific text (that was in the first Word document)
Thanks very much!

You would have to address the document in question after storing the
selected text in a variable - here sText
e.g to follow the code you have already created. Change the information in
sPath = to reflect the document that you want to search.

Sub FindWhatsinClipboard()
Dim sText As String
Dim sPath As String
sPath = "D:\My Documents\Test\Name1.docx"
'store the selected text
sText = Selection.Text
'open the other document
Documents.Open sPath
'The opened document is now the active document
Selection.Find.ClearFormatting
With Selection.Find
.Text = sText ' the stored selection
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub


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