banner for Ms Access Gurus

Clipboard Set, Get, and Clear text using VBA

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).

use VBA to Set, Get, and Clear the Windows Clipboard

Quick Jump

Goto the Very Top  


Download

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

Goto Top  

VBA

Standard module

'*************** 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

Goto Top  

Reference

Microsoft Learn

Help: setData method

Help: getData method

Help: clearData method

Help: IHTMLDataTransfer interface

Goto Top  

Backstory

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.

Goto Top  

Share with others

Here's the link for this page in case you want to copy it and share it with someone:

https://msaccessgurus.com/VBA/Clipboard_MSHTML.htm

or in old browsers:
http://www.msaccessgurus.com/VBA/Clipboard_MSHTML.htm

Goto Top  

the simplest way is best, but usually the hardest to see ~ crystal

Show your appreciation

thank you

Goto Top