VBA Check Clipboard Text Before Pasting

jamesuk

Board Regular
Joined
Sep 8, 2015
Messages
85
Hi All

Hoping someone can help me out, not sure if this is even possible!

I have a simple bit of code that pastes data from the clipboard into a worksheet:

Code:
Sub PasteWOc()




Dim dest As Range
    Set dest = Sheets("wo").Range("a1")
    
    dest.PasteSpecial xlPasteAll
    Application.CutCopyMode = False
    


End Sub

Is it possible to have the code check the first 2 characters of text on the clipboard, and make sure it begins with 'WO' before pasting? if not, exit the sub?

Many thanks for your assistance in advance
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
It is possible. First, go to the VBA Editor. In the menu, click on Tools --> References, then put a check next to Microsoft Forms 2.0 Object library. Then paste this code:
Code:
Sub PasteWOc()
Dim MyText As String

    Set MyData = New DataObject
    MyData.GetFromClipboard
    MyText = MyData.GetText

    If Left(MyText, 2) <> "WO" Then Exit Sub

    Sheets("wo").Range("a1") = MyText

End Sub
That should do it!
 
Upvote 0
Brilliant! thanks Eric :)

can I ask, if someone else was to use the workbook on a different machine, would they too have to make reference to Object Lib for it to work?
 
Upvote 0
No, the reference is saved with the workbook. You can actually skip the reference and late bind the DataObject although the code is a little obscure:
Code:
   Set myData = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
rather than:
Code:
Set MyData = New DataObject
 
Upvote 0
This worked perfectly! thank you! :)

one last question... I'm trying to force Excel to show a msge box and exit the sub if a debug error happens (when the clipboard is empty)

I've been playing with:

Code:
On Error GoTo ErrHandler:


ErrHandler:
 
 MsgBox "hello"
Exit Sub


Resume

but to no avail :(

any suggestions?
Many thanks!
 
Upvote 0
For example:

Code:
Sub PasteWOc()
    Dim MyText                As String
    Dim myData                As Object

    On Error GoTo ErrHandler
    Set myData = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    myData.GetFromClipboard
    MyText = myData.GetText

    If Left(MyText, 2) <> "WO" Then Exit Sub

    Sheets("wo").Range("a1") = MyText

    Exit Sub

ErrHandler:

    MsgBox "hello"

End Sub
 
Upvote 0
Hi RoryA

Thanks! i'll give that a go and let you know how I get on :)

out of my own curiosity, that looks like a Regkey? can you explain to me what purpose that serves in the code?
 
Upvote 0
It's the CLSID for an MSForms.DataObject. It's the only way I know to late bind one.
 
Upvote 0
Hi RoryA

That worked perfectly :) thank you for your help!

Always learn something new when I come here for help! :D
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top