Copy string to windows clipboard VBA

Jurg55

New Member
Joined
Jan 31, 2024
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi,

I would like to have a code that copies a string to the windows clipboard using VBA. So i can paste the clipboard to other software then excel.

My code is now the following:

VBA Code:
    Dim strText As String
    strText = FilePath

    With New DataObject
        .SetText strText
        .PutInClipboard
    End With

If I test the code it works for me, but could you check it and make changes if this code is not how it SHOULD be.

Thank you!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Yes, your code looks fine and works for me too . . .
 
Upvote 0
There may have been problems using DataObject for a long time. Here is another way to copy using Microsoft HTML Object Library.
VBA Code:
Sub Test()
    Dim var1
    Dim var2

    var1 = "Hello world."
    var2 = 123

    'transfer to clipboard
    Clipboard var1 & vbLf & var2

    'read from clipboard
    MsgBox Clipboard
End Sub


Function Clipboard(Optional s As String) As String
    Dim v

    v = s  'for 64-bit.

    With CreateObject("htmlfile")
        With .parentWindow.clipboardData
            Select Case True
                Case Len(s) > 0: .setData "text", v
                Case Else: Clipboard = .GetData("text")
            End Select
        End With
    End With
End Function

Artik
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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