' Copy ClipboardText if not empty to clipboard and return vbnullstring
' Return clipboard text if ClipboardText is empty
' Early bind using ref to Microsoft HTML Object Library
'
Public Function TheClipboard(Optional ByVal ClipboardText As String = vbNullString) As String
'PURPOSE: Read/Write to Clipboard
'Source: ExcelHero.com (Daniel Ferry)
'https://www.thespreadsheetguru.com/blog/2015/1/13/how-to-use-vba-code-to-copy-text-to-the-clipboard
On Error GoTo errTheClipboard
Dim ReturnValue As String
Dim X As Variant 'Store as variant for 64-bit VBA support
X = ClipboardText
' With New HTMLDocument ' Early binding
With CreateObject("htmlfile")
With .parentWindow.clipboardData
Select Case True
Case Len(ClipboardText)
'Write to the clipboard
.SetData "text", X
Case Else
'Read from the clipboard (no variable passed through)
ReturnValue = .GetData("text")
End Select
End With
End With
doneTheClipboard:
On Error Resume Next
TheClipboard = ReturnValue
Exit Function
errTheClipboard:
' LogError "TheClipboard"
Resume doneTheClipboard
End Function