Use VBA to set text, get text, and clear text on the Windows Clipboard. This code works in Access, Excel, Word, PowerPoint, Visio, VBA applications, is fast, and can handle thousands of characters.
Instead of specifying "Text" for the clipboard data format, there are other options such as HTML, Image, and URL. When copying and pasting, rich text is converted to plain text. Uses late binding, so no special reference needed. When run, it loads the Microsoft HTML Object Library (MSHTML).
Download zipped BAS file with module that you can import into Access, Excel, Word, PowerPoint, Visio, and any VBA application mod_Clipboard_MSHTML_s4p__BAS.zip
Download zipped Access ACCDB file with a test form and module Clipboard_240921_s4p__ACCDB.zip
Although the form is in Access and works for the textbox controls there, you can also copy something from another application onto the clipboard. Then when you click the Paste button on the form, it will show THAT. Conversely, you can Copy here and paste to somewhere else ... the Windows clipboard works across all its applications.
If you have trouble with a downloaded file, remember to unblock the original ZIP file, (aka remove Mark of the Web), BEFORE extracting file(s). Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm
'*************** Code Start ***************************************************** ' module name: mod_Clipboard_MSHTML_s4p '------------------------------------------------------------------------------- ' Purpose : VBA to work with Text on the Windows Clipboard ' Set, Get, and Clear ' Author : crystal (strive4peace) ' This code: https://msaccessgurus.com/VBA/Clipboard_MSHTML.htm ' LICENSE : ' You may freely use and share this code, but not sell it. ' Keep attribution. Use at your own risk. '------------------------------------------------------------------------------- ' LATE binding is used, so no reference needed. ' For better preformance, resuse object. ' To explore and for EARLY binding: ' Microsoft HTML Object Library ' ( MSHTML ) Public Sub ClipboardSetText(ByVal pvText As Variant) '240612 strive4peace With CreateObject( "htmlfile") 'New HTMLDocument ' method for interface .parentWindow.clipboardData.setData "Text",pvText End With End Sub Public Function ClipboardGetText() As Variant '240612 With CreateObject( "htmlfile") ClipboardGetText = .parentWindow.clipboardData.getData( "Text") End With End Function Public Sub ClipboardClearText() '240612 With CreateObject( "htmlfile") .parentWindow.clipboardData.clearData ( "Text") End With End Sub '*************** Code End ******************************************************' Code was generated with colors using the free Color Code add-in for Access
Help: setData method
Help: getData method
Help: clearData method
Initially, I used a Windows API to manage the clipboard. Then I used an easier way with MS Forms, but it appears to be deprecated.
So now I'm using the Microsoft HTML Object Library, which works and is fast.