Option Explicit
' Set via VBE-Tools-References the reference to Microsoft HTML Object Library
Function SetClipBoardText(ByVal Text As Variant) As Boolean
With New HTMLDocument
SetClipBoardText = .parentWindow.ClipboardData.SetData("Text", Text)
End With
End Function
Function GetClipBoardText() As String
On Error Resume Next
With New HTMLDocument
GetClipBoardText = .parentWindow.ClipboardData.GetData("Text")
End With
End Function
Function ClearClipBoardText() As Boolean
With New HTMLDocument
ClearClipBoardText = .parentWindow.ClipboardData.clearData("Text")
End With
End Function
' === Tests ===
Sub Test_SetClipBoardText()
Dim Txt As String
Txt = "Any text"
Call SetClipBoardText(Txt)
' Use Ctrl+V to paste text from the Clipboard
End Sub
Sub Test_GetClipBoardText()
' Copy any text via Ctrl+C to the Clipboard, or uncomment the below line
'Call SetClipBoardText("MyText")
Dim Txt As String
Txt = GetClipBoardText()
Debug.Print Txt
End Sub
Sub Test_ClearClipBoardText()
' Copy any text via Ctrl+C to the Clipboard or uncomment the below line
'Call SetClipBoardText("MyTextForTesting")
Dim Ret As Boolean
Ret = ClearClipBoardText
Debug.Print Ret
' Use Ctrl+V to paste empty string from the Clipboard
End Sub