View Full Version : Assign picture to Image Control

11-21-2007, 05:05 PM
I am running xl2K under WinXP. I wish to assign a picture file to an Image control. I can do it manually by creating the Image control (essentially a shape) and then diddling with properties to define the jpg file, picture format, etc. But when I look at the code created via the macro recorder, most of what I did is not there. Controlling the creating, location, size, etc., with VBA is pretty easy, but how do I assign the jpg file and the picture format.

11-22-2007, 06:41 AM
You have to put an image control on sheet 1 with the toolbox and give this the name Person_Image. Then you put names starting from row 2 in column A. Each name has the same filename for your picture. And your pictures are stored under My pictures.Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'In column A we put the names of the person
If Target.Column = 1 And Target.Row > 1 Then
If Target.Value = vbNullString Then
Person_Image.Picture = LoadPicture("")
'The imagecontrol on sheet1 is named Person_Image
With Person_Image
'The name of the file is the same as the name on your worksheet
'and all your pictures are stored under My pictures
.Picture = LoadPicture("C:\Documents and Settings\USERNAME\My documents\" & _
"My pictures\" & Target.Value & ".jpg")
.Height = 100
.Width = 100
'Fit picture to the size you want
.PictureSizeMode = fmPictureSizeModeStretch
End With
End If
End If
End Sub

11-29-2007, 03:15 PM
Thanks for your reply, but your approach is much too specific, i.e., name of ImageControl, how it is created, how it is accessed, etc. Also, no matter what I named the ImageControl, e.g., "Person_Image" or anything else, the

With Person_Image (or whatever I called it) line of code generated an immediate error. Part of the problem is that although the manual method appears to create a ImageControl (and I can fiddle with it manually), the macro recorder code generates a shape that has no "picture" attribute

So let's go back to square one; I need a method to create an ImageControl via VBA. No manual steps.