Be the first user to complete this post

  • 0
Add to List

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