none
require help for syntax to load a picture from spreadsheet into imagebox RRS feed

  • Question

  • Hi, i have troubles trying to find the correct syntax for loading a picture "Picture 1" from my spreadsheet into a imagebox.

    i found some examples

    Me.PictureBox1.Image = Image.FromFile("C:\foo.bmp")

    but these are for loading pictures using a path directory


    how should i load a picture from my spreadsheet into the image box?

    kindly advise.

    Tuesday, January 15, 2013 8:50 AM

All replies

  • Adapt the following

    Sub test()
    Dim sFile As String
    Dim ole As OLEObject
    
    ' add an image control
         Set ole = ActiveSheet.OLEObjects.Add("forms.image.1")
         With Range("B2")
                 ole.Left = .Left
                 ole.Top = .Top
         End With
    
    'or use an existing image control
    '     Set ole = ActiveSheet.OLEObjects("image1")
            sFile = "c:\path\mypicture.jpg"
    
         ole.Object.Picture = LoadPicture(sFile)
            ole.Object.AutoSize = True
    
    End Sub

    Peter Thornton

    Tuesday, January 15, 2013 10:55 AM
    Moderator
  • Hi peter. thanks for your reply.

    may i check if it is possible for me to load an image into a imagebox, when the image is already on a spreadsheet.

    e.g. i pass a image onto cell A1 of sheet 1.

    is there a way to specify image1.picture=LoadPicture(thisworkbook.sheets("Sheet1").cells(1,1))

    Thanks

    Tuesday, January 15, 2013 11:38 AM
  • Sorry I didn't ready your OP carefully. I haven't worked with a Picturebox on a sheet but from a quick search it looks like you can copy a picture to the clipboard then paste to the Picturebox. But you say "imagebox", confirm if you are working with a VS PictureBox or a standard built-in Image control (ie an activeX / oleObject)

    I can't test a PictureBox but describe exactly what you mean by "when the image is already on a spreadsheet". Depending on what you mean it might be simple or difficult to copy the image to the clipboard.

    Peter Thornton

    Tuesday, January 15, 2013 1:23 PM
    Moderator
  • Hi Peter.

    what i have is a new workbook. on the first excel sheet "Sheet1", i inserted a random picture. The picture's name is Picture 1.

    i added a userform, and in it i have a image box, which i inserted via the toolbox controls. I also have a command button.

    what i am trying to do here is when the ocmmand button is clicked, the picture that i had placed within the spreadsheet can be loaded into the image box.

    i had searched through forums and the internet, but i couldnt find syntax to allow me to load a picture from the spreadsheet. most of the examnples i saw were to load pictures from a path directory.

    Thank you.

    Tuesday, January 15, 2013 3:34 PM
  • OK that's more straightforward

    Get Stephen Bullen's PastePicture.zip.

    http://www.oaltd.co.uk/Excel/Default.htm

    Open the xls and drag the module modPastePicture into your project. The code will get the picture in the clipboard and convert it to a standard picture object

    Adapt the following, could include in your command button's click event. Name the objects to suit, or include some method to refer to the relevant objects.

    [vbnet]
    Sub test3()
    Dim pic As Picture
    Dim ole As OLEObject
       Set pic = ActiveSheet.Pictures("Picture 1")
       Set ole = ActiveSheet.OLEObjects("Image1")
       pic.Copy
       ole.Object.Picture = PastePicture
       ole.Object.AutoSize = True
    End Sub
    [vbnet]

    Peter Thornton

    Tuesday, January 15, 2013 4:33 PM
    Moderator
  • Hi peter, thank you so much for your prompt response.

    I have altered the codes you provided to the controls used, and it is working well.

    THANK YOU.

    Wednesday, January 16, 2013 2:37 AM