Excel VBA code to copy a specific string to clipboard

Excel VBA code to copy a specific string to clipboard

Asked on January 11, 2019 in Excel.
Add Comment


  • 3 Answer(s)

         Here, no need to set hint, in macro uses late binding to copy text to the clipboard. Further can fix and move.

    Sub CopyText(Text As String)
       //VBA Macro using late binding to copy text to clipboard.
       //By Justin Kay, 8/15/2014
       Dim MSForms_DataObject As Object
       Set MSForms_DataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
       MSForms_DataObject.SetText Text
       MSForms_DataObject.PutInClipboard
       Set MSForms_DataObject = Nothing
    End Sub
    

    Follow this command

    Sub CopySelection()
        CopyText Selection.Text
    End Sub
    

    Answered on January 11, 2019.
    Add Comment

        UserForm to the VBA project, by using the Non Win32 otherwise by choice add a reference to Microsoft Forms 2 Object Library, now can simplify from sheet or module

    With New MSForms.DataObject
    .SetText "http://zombo.com"
    .PutInClipboard
    End With
    

    Answered on January 11, 2019.
    Add Comment

    The following command will work only along with text, but not to other data types, adding hint to the Microsoft Forms 2.0 Object Library.

    Dim DataObj As New MSForms.DataObject
     
    //Put a string in the clipboard
    DataObj.SetText "Hello!"
    DataObj.PutInClipboard
     
    //Get a string from the clipboard
    DataObj.GetFromClipboard
    Debug.Print DataObj.GetText
    

    Click here to Excel VBA code to copy a specific string to clipboard.

    Answered on January 11, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.