place image activex control

fraufreda

Board Regular
Joined
Oct 14, 2010
Messages
190
hi.
I cannot figure out how to load a picture into an image control.

Code:
image1.picture = Loadpicture(filepath)

but if the picture is just in another sheet and it's named. how can i load it?

Code:
image1.picture = Loadpicture(range("pic"))??????
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Haha. If only. The only way I know is to copy the picture to the clipboard and then create a new StdPicture object from the clipboard contents. Something like this:

Code:
' 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

WBD
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,806
Members
449,337
Latest member
BBV123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top