View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
Terry Pinnell[_4_] Terry Pinnell[_4_] is offline
external usenet poster
 
Posts: 192
Default Getting a variable's contents onto the clipboard

"Peter T" wrote:

"Terry Pinnell" wrote in message
OK, understood, I didn't notice that serious snag, sorry!

In case either of you or anyone else who end up here is interested, here
is my
attempt at pasting the entire thread.


Sorry but it would take too long to go through all that to figure what
you've tried, what's not working, and why not.

Try this basic MS example:

https://docs.microsoft.com/en-us/off...-the-clipboard

This will need to be adapted for use with 64bit Excel, first the API
declarations at the top of the form:

#If VBA7 Then
' works with all versions of 32 or 64 bit 2010 or later
' All the following APIs are likely to be word wrapped after
' posting, should be on eleven single lines each starting Private
Private Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hWnd As
LongPtr) As Long 'Ptr
Private Declare PtrSafe Function EmptyClipboard Lib "User32" () As Long
Private Declare PtrSafe Function CloseClipboard Lib "User32" () As Long
Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "User32"
(ByVal wFormat As Long) As Long
Private Declare PtrSafe Function GetClipboardData Lib "User32" (ByVal
wFormat As Long'Ptr) As LongPtr
Private Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal
wFormat As Long'Ptr, ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalAlloc Lib "kernel32.dll" (ByVal
wFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalLock Lib "kernel32.dll" (ByVal hMem
As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem
As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalSize Lib "kernel32" (ByVal hMem As
LongPtr) As LongPtr
Private Declare PtrSafe Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW"
(ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr

#Else
' for use with Excel 2007 or earlier
' the same APIs as in the MS link
#End If

The declarations in at the top of the two example routines also need to be
adaped:

Public Sub SetClipboard(sUniText As String)
#If VBA7 Then
Dim iStrPtr As LongPtr
Dim iLock As LongPtr
#Else
Dim iStrPtr As Long
Dim iLock As Long
#End If
Dim iLen As Long
' rest of the routine same as the MS link

Public Function GetClipboard() As String
#If VBA7 Then
Dim iStrPtr As LongPtr
Dim iLock As LongPtr
#Else
Dim iStrPtr As Long
Dim iLock As Long
#End If
Dim iLen As Long
Dim sUniText As String
' rest of the routine same as the MS link

If this doesn't do what you want explain why not, with any error messages as
applicable.

Peter T


Thanks, but as mentioned in my later posts in that thread I now have it working.
With code that at a glance looks very similar to yours above.

My original question was seeking expert advice/insights/confirmation on points such
as
"...seems astonishingly complex."
"...seems rather inflexible too."
and particularly any better/simpler methods

Terry, East Grinstead, UK