PDA

View Full Version : put clipboard as variable



danovkos
03-23-2009, 03:51 AM
HI,
pls. i searched on the forum but i wasnt succesfull.

I found something like this code but it doesnt works. I want to find value from clipboard in my huge DB after clicking on button in toolbar. After clicking it show the cell, where the value is – if there is.
Can you help me pls?
thank you


Sub Find_CIF()
Dim MyClipObj As New DataObject
Dim c As Range
Dim obj As New DataObject
Dim pval As String

'Temporarily save clipboard data
obj.GetFromClipboard
pval = obj.GetText

If pval = "" Then Exit Sub

Set c = Sheets("vývoj").Cells.Find(What:=pval, LookIn:=xlFormulas, LookAt:=xlWhole)
If Not c Is Nothing Then Application.Goto c
End Sub

Kenneth Hobs
03-23-2009, 05:44 AM
More likely the problem is with your find. If you debug does pval equal ""?.

'Requires Tools > References > Microsoft Forms 2.0 Object Library
Sub Find_CIF()
Dim MyClipObj As New DataObject
Dim c As Range
Dim obj As New DataObject
Dim pval As String

'Temporarily save clipboard data
obj.GetFromClipboard
pval = obj.GetText
Debug.Print pval
End Sub

danovkos
03-23-2009, 06:12 AM
sorry i dont understund.

I try your code, but it return error.

with this
equal ""?. i mean, that if is clipboard empty = exit sub
but this code return error "user defined type not defined"

Kenneth Hobs
03-23-2009, 06:16 AM
Use the code that I posted, not the text of what I said as code. Debug.Print puts the value in the Immediate Window of the VBE. Or, just press F8 and when it sets the pval, hover the cursor over pval to see the value.

danovkos
03-23-2009, 06:30 AM
sorry i dont understund. I never do something like this with code.
I tried it run in debugger with F8 – steb by step, but it return error immediately on start of code
i tried paste your code to workbook and in module too, but i dont know how it works :(

Kenneth Hobs
03-23-2009, 07:19 AM
Error messages tell you what is wrong. Telling us it errors without those details tells us nothing.

Did you add the reference as I listed as the first line commented in my code?

If you are still stuck, post a sample xls by attaching it. Click the paperclip icon to attach it.

danovkos
03-23-2009, 07:31 AM
ok
now i tried it with my little modification and it works, if i have value from other application. I mean, f.e. if i copy value from text file to clip and try to find with my code it now works. But if i copy any value and the value is actualy copied it doesnt search - it found nothing


Private Sub CommandButton14_Click()
Dim MyClipObj As New DataObject
Dim c As Range
Dim obj As New DataObject
Dim pval As String

On Error GoTo 0
'Temporarily save clipboard data
obj.GetFromClipboard
pval = obj.GetText
On Error GoTo 0
Set c = Sheets("vývoj").Cells.Find(What:=pval, LookIn:=xlFormulas, LookAt:=xlPart)
If Not c Is Nothing Then Application.Goto c
On Error GoTo 0
Debug.Print pval
End Sub

Kenneth Hobs
03-23-2009, 09:39 AM
Most people LookIn xlValues, not xlFormulas.

Without something to test, it is hard for us to troubleshoot.

danovkos
03-25-2009, 05:20 AM
OK,
here is simply file. But i dont know if it helps.
I have your macro in modul (i want after finishing this code, insert it to my personal.xls module. I run it from toolbar, becasue this is what i need. I copy something to clipboard - f.ex. from mail and click on button in toolbar. I want search the content of clipboard in active sheet. And i want to use it if i copy somehting from f.e. txt file or if i copy it from other excel sheet.
Now it return errror:
compile error - User defined-type not defined
and stops on row in code

MyClipObj As New DataObject

Kenneth Hobs
03-25-2009, 07:20 AM
Again, you did not set the reference to the object. If you don't know how to set references, you should learn. I set it for you in your example. I also changed it to looking the xlValues as I explained earlier.

danovkos
03-26-2009, 01:30 AM
i tried it. I have checked "Microsoft Forms 2.0 Object Library", so i thing it is used. But it works only for value which is coppied from other as excel. F.e. if i copy text from outlook mail it find it, but if i copy something from Excel and click on button, it doesnt works. Do i something wrong?
thx