Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

VBA-Excel: Putting Text In The Windows Clipboard

For working with Windows Clipboard you need DataObject, the object in MSForms library. It provides support for text-string.
For that you must add the reference “Microsoft Forms 2.0 Object Library”
How to add “Microsoft Forms 2.0 Object Library”
Now, For putting text in the Clipboard, Follow the below steps.
Steps:
•    Initialize the Data Object, the type of MSForms.DataObject
•    Create a String
•    Set the String into Data Object using SetText() method.
•    Put the data in ClipBoard using PutInClipboard

Initialize the Data Object, the type of MSForms.DataObject
Dim objData As New MSForms.DataObject
Create a String
strText = “I will be in ClipBoard”
Set the String into Data Object using SetText() method.
objData.SetText strText
Put the data in ClipBoard using PutInClipboard
objData.PutInClipboard

Complete Code:

Function FnPutDataInClipBoard()

    Dim objData As New MSForms.DataObject
    Dim strText

    strText = "I will be in ClipBoard"
    objData.SetText strText
    objData.PutInClipboard

End Function 

NOTE: Now if you open the Notepad and press Ctrl+V, you can see that string you had put in, gets pasted in the Notepad

Read about:

Get Text from the Windows Clipboard

Storing multiple data In the Windows Clipboard

 

You may also like...

9 Responses

  1. Nick Warren says:

    Thanks for posting this, it’s the only solution I’ve found that actually works. I included and activated the cell I wanted to copy from above the code and changed the item in “” to Activecell and bingo.

  2. John Frim says:

    Sumit,

    The macro stuff above is almost perfect for my needs. The only twist is that I want the text that is going to the clipboard to be taken from the ActiveCell on a worksheet — but not the entire cell content, rather just what I have highlighted in the cell. If I do Command-C to manually copy to the clipboard I get what I want and can paste from the clipboard. I would like to just click on a cell, drag over the text snippet that I want to copy, and then click a button to put that text into the clipboard. Thanks for any assistance.

    J

  3. Unfortunately, there is a bug in Windows 8.1 that stops this from working. You only ever end up with “??” in the clipboard.

    • SJ says:

      Hello Julian, Checked on windows 8.1, it is working fine. Make sure the reference How to add “Microsoft Forms 2.0 Object Library” is added. Article has a link how to add it as well.

  4. I’ve seen the same problem reported by Julian whereby “??” is returned from the clipboard when using Ctrl+V even if the .GetText method returns what was put in the clipboard. This used to work in code I wrote several years ago and I just noticed it failing in this way on Windows 10 x64 + MSO 2016 x32. It seems that Microsoft may have broken something as the project reference for MSForms 2.0 is present and correct and the identical project code works on Windows 7.

  5. BB says:

    Have the same issue with Excel 2013 and Windows 10. Just getting gibberish in the clip board.
    Made sure that I have “Microsoft Forms 2.0 Object Library” added in the references.

  1. April 2, 2015

    […] document.write(''); My mistake. To use the clipboard you need to code differntly. See Windows Clipboard VBA-Excel: Putting Text In The Windows Clipboard […]

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: