简体   繁体   中英

Get Picture from Worksheet into Excel Userform

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.

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.

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 (eg, a JPEG), and then use LoadImage() to pull the image back into the image control.

I know this post is ancient, but I found my way here. I came up with a slightly different solution to this problem. I have about 30 pictures I need to load based on a combo-box selection. First, all combo-box options (for discussion "XX") are saved to a separate worksheet which is "Very hidden" from the user and loaded into the combobox on userform activation. On the userform a frame was added, and within the frame 30 image-boxes all overlapping perfectly were placed. Each image-box was carefully named "Img_XX" where XX is the simple two-letter identifier.

With this setup it is now possible to iterate through each "Control" (the image boxes) in the Frame and hide them all, except the one with a name that matches the combo-box value. The code in the userform module, within the Combobox_Change() function, looks something like this:

 Private Sub ComboBox_Change()
    Dim SearchValue as String
    SearchValue = me.Combobox.value

    Dim Ctrl as Control
    For each Ctrl in Me.TestFrame.Controls
        If Ctrl.Name Like "img_" & SearchValue Then
            Ctrl.visible = True
        else
            Ctrl.Visible = False
        End If
    next Ctrl
 End Sub

I hope this helps, let me know what you think. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM