Copy to Clipboard

richardlp

New Member
Joined
Nov 24, 2020
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
In VBA I am having issues copying a string to clipboard. I have read most post; however, I get error messages. I am on a company computer, so I am assuming that there is some security setting causing my issue.

So, hopefully somebody might be able to help me out. Currently, I am trying to us API code. I am getting message "Can't find DLL entry point GlobalAlloc in kernel32.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
In Excel vba I have always used the forms data object without too much trouble. Not sure what you are doing with api code - could be a 32 bit vs 64 bit thing ...
Example of data object clipboard:
 
Upvote 0
Thank you; however, when I paste, there is nothing that paste. I know the notepad file received something because it is asking me if I want to save while closing. Otherwise, notepad is blank.
 
Upvote 0
Hi,
Try the solution from Rory - VBA Post to Clipboard
In some configurations DataObject doesn't work if Explorer's window is open, it seems an old bug (2013+) but it's still here.
 
Upvote 0
Hi ZVI,

I get message, "Run-time error '429'. ActiveX component can't create object" upon running the code.
 
Upvote 0
Hi Richard,
Try using early binding instead of late binding.
In VBE-Tools-References set the reference to Microsoft HTML Object Library and use the below code:
VBA Code:
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
An alternative method is using API calls, but it is not compact.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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