ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Getting a variable's contents onto the clipboard (https://www.excelbanter.com/excel-programming/454546-getting-variables-contents-onto-clipboard.html)

Peter T[_8_] January 13th 20 05:36 PM

Getting a variable's contents onto the clipboard
 
"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



Terry Pinnell[_4_] January 13th 20 06:26 PM

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

GS[_6_] January 13th 20 10:25 PM

Getting a variable's contents onto the clipboard
 
"GS" wrote in message
Yes, I've been using this in XP/W7. Haven't tried it on W10 but Peter's
suggestion to use the APIs makes sense given that DataObject is broken in
W10!


It's not particularly broken in W10 and it's fine in all my systems, incl
x64. The bug seems to affect some systems dating back to 2010x64 in W7,
further back than I recalled earlier. No excuse as I was involved in one of
the early threads about it, though as I couldn't repro it took a while to
appreciate there really was a bug.

Peter T


And, as we both know, it's always more reliable to work directly with APIs
given the nuances of VBA6/7 and the latter's PtrSafe requirements.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

GS[_6_] January 13th 20 10:29 PM

Getting a variable's contents onto the clipboard
 
Much better tutorial than I have, so it's "out with the old, in with the new"
for me, replacing my existing .bas!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

Peter T[_8_] January 13th 20 11:01 PM

Getting a variable's contents onto the clipboard
 
"Terry Pinnell" wrote in message
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


API approaches can seem complex particularly if new to using APIs, and this
example perhaps more than most because different things are going on as part
of the whole operation. I guess your choice is just use it as offered or if
interested research how APIs work in general and these in particular.

However not sure why you describe it as inflexible. Quite the reverse, it's
far more flexible than say using the DataObject which is limited to text
only. With the APIs you can work with anything the clipboard can accept, and
retrieve information about the 'type' of data that's in the clipboard before
deciding to 'get' it. That said you would need to adapt the examples for use
with other data types.

As for any better methods - not really / simpler - indeed use the
DataObject!

Peter T



Peter T[_8_] January 13th 20 11:11 PM

Getting a variable's contents onto the clipboard
 

"GS" wrote in message ...
"GS" wrote in message
Yes, I've been using this in XP/W7. Haven't tried it on W10 but Peter's
suggestion to use the APIs makes sense given that DataObject is broken
in W10!


It's not particularly broken in W10 and it's fine in all my systems, incl
x64. The bug seems to affect some systems dating back to 2010x64 in W7,
further back than I recalled earlier. No excuse as I was involved in one
of the early threads about it, though as I couldn't repro it took a while
to appreciate there really was a bug.

Peter T


And, as we both know, it's always more reliable to work directly with APIs
given the nuances of VBA6/7 and the latter's PtrSafe requirements.


VBA7 doesn't actualy 'require' PtrSafe APIs, merely it can compile and use
them even in 32bit. It's Win64 that requires PtrSafe APIs and LongPtr (in
effect LongLong in x64) if/as necessary. Sorry if this sounds like
semantics...;)

Peter T



GS[_6_] January 13th 20 11:34 PM

Getting a variable's contents onto the clipboard
 
"GS" wrote in message ...
"GS" wrote in message
Yes, I've been using this in XP/W7. Haven't tried it on W10 but Peter's
suggestion to use the APIs makes sense given that DataObject is broken in
W10!

It's not particularly broken in W10 and it's fine in all my systems, incl
x64. The bug seems to affect some systems dating back to 2010x64 in W7,
further back than I recalled earlier. No excuse as I was involved in one
of the early threads about it, though as I couldn't repro it took a while
to appreciate there really was a bug.

Peter T


And, as we both know, it's always more reliable to work directly with APIs
given the nuances of VBA6/7 and the latter's PtrSafe requirements.


VBA7 doesn't actualy 'require' PtrSafe APIs, merely it can compile and use
them even in 32bit. It's Win64 that requires PtrSafe APIs and LongPtr (in
effect LongLong in x64) if/as necessary. Sorry if this sounds like
semantics...;)

Peter T


I already know this but thanks for clarifying for other readers, though!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


All times are GMT +1. The time now is 01:31 AM.

Powered by vBulletin® Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
ExcelBanter.com