' GUID
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(8) As Byte
End Type
' Structure for creating an image
Private Type PICTDESC
cbSize As Long
picType As Long
hImage As Long
End Type
' Windows API functions
Private Declare Function OpenClipboard& Lib "user32" (ByVal hwnd&)
Private Declare Function GetClipboardData& Lib "user32" (ByVal wFormat%)
Private Declare Function CloseClipboard& Lib "user32" ()
Private Declare Function CopyImage& Lib "user32" (ByVal handle&, ByVal un1&, ByVal n1&, ByVal n2&, ByVal un2&)
Private Declare Function IIDFromString Lib "ole32" (ByVal lpsz As String, ByRef lpiid As GUID) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32" (pPictDesc As PICTDESC, ByRef riid As GUID, ByVal fOwn As Long, ByRef ppvObj As IPicture) As Long
Private Sub CopyNamedPictureToImage(pictureName As String, targetImage As Image)
Const IPictureIID = "{7BF80981-BF32-101A-8BBB-00AA00300CAB}"
Dim iPic As IPicture, tIID As GUID, tPICTDEST As PICTDESC, Ret&
Dim hCopy&
' Copy the picture in the named picture
Sheet1.Shapes(pictureName).CopyPicture 1, 2
' Get a handle to the image on the clipboard and take a copy
OpenClipboard 0&
hCopy = CopyImage(GetClipboardData(2), 0, 0, 0, &H4)
CloseClipboard
If hCopy = 0 Then Exit Sub
' Get the IID of the picture
Ret = IIDFromString(StrConv(IPictureIID, vbUnicode), tIID)
If Ret Then Exit Sub
' Set up the parameters to create the picture
With tPICTDEST
.cbSize = Len(tPICTDEST)
.picType = 1
.hImage = hCopy
End With
' Create the picture from the handle we have
Ret = OleCreatePictureIndirect(tPICTDEST, tIID, 1, iPic)
If Ret Then Exit Sub
' Put the image into the
targetImage.Picture = LoadPicture("")
targetImage.Picture = iPic
' Destroy the picture
Set iPic = Nothing
End Sub
Public Sub Test()
CopyNamedPictureToImage "pic", Sheet1.Image1
End Sub