Load a pic into ActiveX Image control

MartinK

Active Member
Joined
Oct 30, 2003
Messages
384
Hello,

I have inserted an Image ActiveX control on my sheet. I can load jpgs in it by choosing Properties - Picture.

I cannot find a way how to load the picture based on a value in a different cell, e.g. A1 = Test1, load Test1.jpg, etc. The Intellisense and AuroList members is betraying me, as usual.

BTW, does anbody have a good link about how the Autolisting help when writting the code helps? Sometimes I write a period (dot) and the members are listed. Sometimes not (like in this case).

Thank you
Martin
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

bruno.trudo

Board Regular
Joined
Jan 14, 2004
Messages
98
You can try this code:
Code:
Private Sub Image1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Image1.Picture = LoadPicture(Application.GetOpenFilename)
End Sub
It opens an openfile dialog when you doubleclick the image1.
You can change Application.GetOpenFilename to Range("A1").Value if it contains full path including filename

Pomůže to? :)
 

MartinK

Active Member
Joined
Oct 30, 2003
Messages
384
Thanks! Works a treat! (y)
I just tweaked it to:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, [a1]) Is Nothing Then
        Image1.Picture = LoadPicture([a1].Value)
    End If
End Sub
Martin

Dík :biggrin:
 

Ekim

Well-known Member
Joined
Jun 30, 2002
Messages
1,416
Martin,

Here’s another idea.

Your list of JPEG descriptions in A2:A10, without the file extension.

Image ActiveX control – Properties:
BackColor: White
BorderStyle: None
PictureAlignment: Center
PictureSizeMode: fmPictureSizeModeZoom

Macro:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim WS As Worksheet
Dim mPath As String
If Not Application.Intersect(Range("A2:A10"), Target) Is Nothing Then
    mPath = "C:\Documents and Settings\My Pictures\" & Target.Text & ".jpg"
    If Dir(mPath) <> "" Then
        Set WS = ActiveSheet
        WS.OLEObjects("Image1").Object.Picture = LoadPicture(mPath)
    End If
End If
End Sub
Single click an image description in the List.

Regards,

Mike
 

MartinK

Active Member
Joined
Oct 30, 2003
Messages
384

ADVERTISEMENT

Brilliant! Thank you!
Why this:
Code:
If Dir(mPath) <> "" Then
?
To handle the error if no file in the folder?
Martin
 

MartinK

Active Member
Joined
Oct 30, 2003
Messages
384
OK, now this is a silly one...
I do not want to save the pic together with the sheet.
How to I "clear" the Image control?
Thanks
Martin
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209

ADVERTISEMENT

Code:
Image1.Picture = LoadPicture("")
 

Ekim

Well-known Member
Joined
Jun 30, 2002
Messages
1,416
Ivan,

Could not get that to work. Errors out with “Object required”. This works:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim WS As Worksheet
Set WS = Sheets(1)

With WS
   .OLEObjects("Image1").Object.Picture = LoadPicture("")
End With

End Sub

Regards,

Mike
 

Anigito

New Member
Joined
Jul 16, 2011
Messages
6
Martin,

Here’s another idea.

Your list of JPEG descriptions in A2:A10, without the file extension.

Image ActiveX control – Properties:
BackColor: White
BorderStyle: None
PictureAlignment: Center
PictureSizeMode: fmPictureSizeModeZoom

Macro:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim WS As Worksheet
Dim mPath As String
If Not Application.Intersect(Range("A2:A10"), Target) Is Nothing Then
    mPath = "C:\Documents and Settings\My Pictures\" & Target.Text & ".jpg"
    If Dir(mPath) <> "" Then
        Set WS = ActiveSheet
        WS.OLEObjects("Image1").Object.Picture = LoadPicture(mPath)
    End If
End If
End Sub
Single click an image description in the List.

Regards,

Mike

Thank you for sharing the knowledge.

I have a question, if I have a list of image URLs which is concatenated from the data of the Excel Table, can I do the same, please?

The alteration to "mPath = "C:\D ... " string is required, I guess. Will you be so kind as to help me with this?
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,161
Messages
5,768,539
Members
425,480
Latest member
br400821

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
Top