Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Find function to pick up clipboard contents

Status
Not open for further replies.

carl777

Instructor
Oct 1, 2002
20
US
I'm trying to edit a recorded Word 2000 macro to pick up the contents of the clipboard (a two-letter string). Maybe this isn't the right way to go about it, but is there a simple way to write something like .Text = ClipBoard.Paste within the With Selection.Find section?
 
Hi carl,

The following info came from (Microsoft Valued Professionals web site):

This is how to get the text on the clipboard into a string variable:
Code:
Dim MyData As DataObject
Dim strClip As String

Set MyData = New DataObject
MyData.GetFromClipboardstrClip
strClip = MyData.GetText
This is how to get the text from a string variable into the clipboard:
Code:
Dim MyData As DataObject
Dim strClip As String
strClip = "Hi there"

Set MyData = New DataObject

MyData.SetText strClip 
MyData.PutInClipboard
I hope this helps, SteveB.
 
Hi StevePB,

Thanks for your response, but when I put

Dim MyData As DataObject and
Set MyData = New DataObject

at the top of the module, I get
"User-defined type not defined" erors on both of those statements when I try to run the macro.

I assume that I embed the

MyData.GetFromClipboardstrClip and the
strClip = MyData.GetText

statments right after the relevant

Selection.Copy statement

and then, finally, use

.Text = strClip

within the relevant With Selection.Find statement??

Carl777
 
NOT YET ANSWERED - More info on this problem:
In my Word 2000 macro, I need the line .Text = "Sc" below to reflect the current and changing contents of the windows clipboard, which for my purposes will be just 2 letters of the alphabet, but not necessarily “Sc”.

Selection.Copy
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Sc"
.Wrap = wdFindContinue
.Format = True
End With

 
I am trying this too...and I get an error message with
Set MyData=New DataObject

I am also struggling with how to get text from the clipboard into word?

Any other suggestions?
 
Hi Carl and Wigmore,

It appears that this code only works on a form. In fact as far as I can see, the DataObject only works on a form.

If you search the VBA help files for "DataObject", there's a lot of info on using it with the clip board, but all the examples say to put the code in the declarations section of a UserForm. (There should be a way to do this outside of a form, but I can't find any references to it.)

Carl - to get the current contents of the clipboard in your code you should be able to use:

.Text = MyData.GetText

but again, this would have to be in the UserForm code in order to access the DataObject called MyData.

Good luck, SteveB.
 
2 solutions were offered to me that both work (Thank you):

1)pick it the contents of the clipboard with
myString = Selection.Text (right after Selection.Copy)
spit it out where needed with .Text = myString
THOSE ARE THE ONLY EXTRA 2 LINES I NEEDED!!

2) If you also want to use all that DataObject stuff, then I think you must also do the following: In the VBA editor, click Tools->References->and make sure that the box to the left of "Microsoft Forms 2.0 Object Library" is checked
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top