Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Picture from Worksheet into Excel Userform

Tags:

excel

vba

I am looking to view an image from the worksheet in an Image control on a userform.

This image will change based on the value on a combobox. I have inserted (Using: Insert -> Pictures) a couple of images into "Sheet1" of my workbook and named them "Picture1" & "Picture2".

I have created the below UserForm:

Form http://im56.gulfup.com/msKyqi.png

And this is the code that I am trying to use in order to load the images from the sheet, but unfortunately, this is not working at the moment.

Private Sub ComboBox1_Change()

UserForm1.Image1.Picture = LoadPicture(Worksheets("Sheet1").Shapes(ComboBox1.Value))

End Sub

Private Sub UserForm_Initialize()

UserForm1.ComboBox1.Clear
UserForm1.ComboBox1.AddItem "Picture1"
UserForm1.ComboBox1.AddItem "Picture2"

UserForm1.ComboBox1.Value = "Picture1"

UserForm1.Image1.Picture = LoadPicture(Worksheets("Sheet1").Shapes(ComboBox1.Value))

End Sub

Every time I run this code I get the below error:

Error http://im43.gulfup.com/YoWvTp.png

Please advise.

like image 525
CaptainABC Avatar asked May 23 '14 20:05

CaptainABC


2 Answers

I figured it out!

As I am using a UserForm there is a workaround to the issue.

Instead of having the images in the worksheet to then try and load them in the form I tried having them in the UserForm in the first place, here is how.

Create a frame on your userform: Frame http://im88.gulfup.com/Moy8I6.png

Set the visible property of the frame to "False": Visible http://im88.gulfup.com/sAIQqh.png

Insert your images by adding a picture control and loading the images, you can add as many images as you need: Images http://im88.gulfup.com/oas0EQ.png

Name the images: Name http://im88.gulfup.com/cIO317.png

Drag all the images one over the other into the frame, (you can then move the frame into a corner so it doesn't bother you:

Drag http://im88.gulfup.com/1fOSut.png Move Away http://im88.gulfup.com/Q1fzKd.png

Next create a picture control, this is what you will use to display the picture based on a selection:

Form View http://im88.gulfup.com/X1UVRB.png

In this example, I am going to use a combobox for the selection. Now insert the below code in to the form which is pretty straight forward:

    Private Sub ComboBox1_Change()

    ' Image1 is the name of the created picture control
    UserForm3.Controls.Item("Image1").Picture = UserForm3.Controls.Item(UserForm3.ComboBox1.Value).Picture 

    End Sub

    Private Sub UserForm_Initialize()

    UserForm3.ComboBox1.AddItem "Argentina"
    UserForm3.ComboBox1.AddItem "Brazil"
    UserForm3.ComboBox1.AddItem "Chile"

    End Sub

As you will see, the frame with the pictures is Hidden, and the image is changing inside the picture control based on a selection:

Result http://im88.gulfup.com/MSqyHF.png

I think it's the better way to go as opposed to exporting the images from the worksheet to a Temp folder and then loading them back into the picture controls.

like image 95
CaptainABC Avatar answered Nov 14 '22 17:11

CaptainABC


The LoadImage() function expects a filename (which can be fully qualified with a drive letter and path). You are getting a type mismatch because it wants a string, and you are giving it an image object.

There is, as far as I know, no simple way to put an image that resides in the current application into an image control. The (hackish) workaround that I know about is to export the image to a file, and then import that same file using LoadImage().

This is the same path you have to go down if you want to embed a chart that updates dynamically into a userform. You export the chart as an image (e.g., a JPEG), and then use LoadImage() to pull the image back into the image control.

like image 3
Verdigris97 Avatar answered Nov 14 '22 15:11

Verdigris97