declaration des fonctions dans diverses versions de EXCEL
Bonjour, bonne année à tous, santé et bonheur,
J'utilise un bouton copier coller grâce à une fonction que j'ai récupéré sur le net.
Je me débrouille avec le VBA mais là je ne comprends pas mon message d'erreur.
Mon bouton appelle la fonction COPY1 ci dessous et fonctionne jusqu'à la version 64 bits.
Je viens de lire sur internet que la formulation des déclarations de variable avait changé et qu'il fallait le prévoir dans notre code.
J'ai donc corrigé mon code comme ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| Option Explicit
#If VBA7 Then
Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare PtrSafe Function CloseClipboard Lib "User32" () As Long
Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Declare PtrSafe Function EmptyClipboard Lib "User32" () As Long
Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
#Else
#If Win64 Then
Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As Longlong) As Longlong
Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As Longlong) As Longlong
Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Longlong, ByVal dwBytes As Longlong) As Longlong
Declare PtrSafe Function CloseClipboard Lib "User32" () As Longlong
Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As Longlong) As Longlong
Declare PtrSafe Function EmptyClipboard Lib "User32" () As Longlong
Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As Longlong, ByVal hMem As Longlong) As Longlong
#Else
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Declare Function EmptyClipboard Lib "User32" () As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
#End If
#End If
Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096
Function Copy1(mystring As String)
Dim hGlobalMemory As Long, lpGlobalMemory As Long
Dim hClipMemory As Long, x As Long
' Allocate moveable global memory.
'-------------------------------------------
hGlobalMemory = GlobalAlloc(GHND, Len(mystring) + 1)
' Lock the block to get a far pointer
' to this memory.
lpGlobalMemory = GlobalLock(hGlobalMemory)
' Copy the string to this global memory.
lpGlobalMemory = lstrcpy(lpGlobalMemory, mystring)
' Unlock the memory.
If GlobalUnlock(hGlobalMemory) <> 0 Then
MsgBox "Impossible de déverrouiller emplacement de mémoire. Copie abandonnée."
GoTo OutOfHere2
End If
' Open the Clipboard to copy data to.
If OpenClipboard(0&) = 0 Then
MsgBox "Impossible d'ouvrir le Presse-papiers. Copie avorté"
Exit Function
End If
' Clear the Clipboard.
x = EmptyClipboard()
' Copy the data to the Clipboard.
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
OutOfHere2:
If CloseClipboard() = 0 Then
MsgBox "Impossible de fermer le Presse-papiers."
End If
End Function |
Malheureusement j'obtient le message "impossible de déverouiller emplacement de mémoire copie abandonnée" qui est le message lorsque la variable GlobalUnlock est différent de 0 sur la version 64 bits uniquement.
Mon PC fonctionnant avec la version 32 bits fonctionne très bien.
J'ai remplacé mes variables longlong par longptr sans succès, même erreur !
D'où viens le problème maintenant ?:(
Merci pour votre aide,
j'ai trouvé une fonction qui me semble pas mal
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| Function Clipboard(Optional StoreText As String) As String
'Pour copier Clipboard ActiveCell
'Pour coller MsgBox Clipboard
Dim x As Variant
x = StoreText 'Stocker comme variante pour la prise en charge de VBA 64 bits
'Créer un objet HTMLFile
With CreateObject("htmlfile")
With .parentWindow.clipboardData
Select Case True
Case Len(StoreText)
'Écrire dans le presse-papiers
.setData "text", x
Case Else
'Lire depuis le presse-papiers (aucune variable transmise)
Clipboard = .GetData("text")
End Select
End With
End With
End Function |
je la trouve pas mal et sans api.
Par contre pourquoi une fonction, je ne vois pas de renvoie ?.
Je viens d'essayer de la transformer en sub mais pas possible ? Clipboard est un appel particulier ?
test nouvelle fonction positif
Bonjour,
cette fonction marche pour toute les versions et plus d'API.
Par contre je ne comprends pas pourquoi il faut une fonction là ?
On ne renvoie pas de données !
Si quelqu'un a une réponse je veux bien analyser
merci pour vos aides