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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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? :)
 
Upvote 0
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:
 
Upvote 0
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
 
Upvote 0
Brilliant! Thank you!
Why this:
Code:
If Dir(mPath) <> "" Then
?
To handle the error if no file in the folder?
Martin
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,214,577
Messages
6,120,359
Members
448,956
Latest member
Adamsxl

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