I'm using 64bit office in 64bit window 10 PC
I did googling and found below sample code to load picture from current clipboard to userform
but not working !!! Firstly I doubt about the dll referencing because I can't manually add reference using tools > reference command in VBE..
So I used regsvr32 C:\windows\system32 oleaut32.dll in administrator mode and now succeeded but still can't get clipboard image to userform..
I changed a little code and simplified it and below is the final one. I'll appreciated it if anybody can give me a simple clue.
I attached the sample file with userform
==================================================================
'Option Private Module
Option Explicit
Option Compare Text
Public Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Public Type uPicDesc
Size As Long
Type As Long
hPic As LongPtr
hPal As LongPtr
End Type
Public Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Public Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As LongPtr 'Correct wFormat type is integer or long???
Public Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Public Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
' In here, I tried manual referencing via tools > reference but can't, the error says
' "Can't add a reference to the specified file"
' Or can I use regsvr32 oleaut32.dll command in cmd window instead ??
' I already did regsvr32 registration and succeeeded but function still not working .
Public Declare PtrSafe Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As LongPtr, ByVal lpszFile As String) As LongPtr
Public Declare PtrSafe Function CopyImage Lib "user32" (ByVal Handle As LongPtr, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As LongPtr
Const CF_BITMAP = 2
Const CF_PALETTE = 9
Const CF_ENHMETAFILE = 14
Const IMAGE_BITMAP = 0
Const LR_COPYRETURNORG = &H4
Public Function PastePicture(Optional lXlPicType As Long = xlPicture) As IPicture
PastePictureVBA7 (lXlPicType)
End Function
Public Function PastePictureVBA7(Optional lXlPicType As Long = xlPicture) As IPicture
Dim H As Long, hPicAvail As Long, hPtr As LongPtr, hPal As LongPtr, lPicType As Long, hCopy As LongPtr ''Correct lPicType type is integer or long???
lPicType = IIf(lXlPicType = xlBitmap, CF_BITMAP, CF_ENHMETAFILE)
hPicAvail = IsClipboardFormatAvailable(lPicType)
If hPicAvail <> 0 Then
H = OpenClipboard(0&)
If H > 0 Then
hPtr = GetClipboardData(lPicType)
If lPicType = CF_BITMAP Then
hCopy = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
Else
hCopy = CopyEnhMetaFile(hPtr, vbNullString)
End If
H = CloseClipboard
If hPtr <> 0^ Then Set PastePictureVBA7 = CreatePictureVBA7(hCopy, 0, lPicType)
End If
End If
End Function
Public Function CreatePictureVBA7(ByVal hPic As LongPtr, ByVal hPal As LongPtr, ByVal lPicType) As IPicture
Dim r As Long, uPicInfo As uPicDesc, IID_IDispatch As GUID, IPic As IPicture
Const PICTYPE_BITMAP = 1
Const PICTYPE_ENHMETAFILE = 4
With IID_IDispatch
.Data1 = &H7BF80980
.Data2 = &HBF32
.Data3 = &H101A
.Data4(0) = &H8B
.Data4(1) = &HBB
.Data4(2) = &H0
.Data4(3) = &HAA
.Data4(4) = &H0
.Data4(5) = &H30
.Data4(6) = &HC
.Data4(7) = &HAB
End With
With uPicInfo
.Size = Len(uPicInfo)
.Type = IIf(lPicType = CF_BITMAP, PICTYPE_BITMAP, PICTYPE_ENHMETAFILE)
.hPic = hPic
.hPal = IIf(lPicType = CF_BITMAP, hPal, 0)
End With
r = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, 1, IPic)
'Is there somting wrong in here ???
If r = 0 Then
Set CreatePictureVBA7 = IPic
End If
End Function
Bookmarks