locked
Insert image RRS feed

  • Question

  • Hi,

    I have one excel sheet(Sheet2) where I have  2 commandbuttons(Browse & Submit). If I click on browse and select any picture(only .jpg)followed by clicking on Submit button that picture should be saved in D1 column of Sheet3. Any help would be appreciated.

    Thanks


    • Edited by DebChatt Sunday, March 8, 2015 4:18 PM
    Sunday, March 8, 2015 4:16 PM

Answers

  • Hi

    Try this macro.

    Sub AddPict()
        Dim strFile As String, rng As Range, pic As Variant
        Set rng = Sheets("Sheet3").Range("D1")
        With Application.FileDialog(msoFileDialogOpen)
            .Filters.Add "Pictures", "*.jpg", 1
            .InitialFileName = "C:\Users\Cimjet\Pictures" 'Change this line to the proper path.
            .Show
            If .SelectedItems.Count = 0 Then Exit Sub
            strFile = .SelectedItems(1)
            pic = strFile
        End With
        Set pic = Sheets("Sheet3").Pictures.Insert(strFile)
        With pic
            .Top = rng.Top
            .Left = rng.Left
        End With
    End Sub

    You only need one Button on your worksheet to run the macro and select the picture


    Cimjet

    Sunday, March 8, 2015 9:10 PM
  • Hi DebChatt,

    Yes, I did try it. I can see one single button is doing the trick. But I want One(Browse) will select the image and other one(Submit) will store the image in Sheet3. It was my mistake to call it userform. I am performing this in an excel sheet.

    Thanks

    If we can separte the procedure into two. For the "Browse" button click we can get the image path and save the value to a variable. And the "Submit" button click we can use that variable to insert the file. And here is an sample for your reference:

    Dim strFile As String
    
    Private Sub Browse_Click()
    Dim rng As Range, pic As Variant
        
         With Application.FileDialog(msoFileDialogOpen)
             .Filters.Add "Pictures", "*.jpg", 1
             .InitialFileName = "C:\Users\UserName\Pictures" 'Change this line to the proper path.
             .Show
             If .SelectedItems.Count = 0 Then Exit Sub
             strFile = .SelectedItems(1)
             pic = strFile
         End With
    End Sub
    
    Private Sub Submit_Click()
      Set rng = Sheets("Sheet3").Range("D1")
         Set pic = Sheets("Sheet3").Pictures.Insert(strFile)
         With pic
             .Top = rng.Top
             .Left = rng.Left
         End With
    End Sub
    
    Also here are some helpful links for you learning Excel developing:

    Excel 2013

    Welcome to the Visual Basic for Applications language reference for Office 2013

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 11, 2015 9:46 AM
  •  Hi DebChatt,

    As far as I test, this issue only exits when the strFile is not initialized.

    After I click the browser button and the strFile is initialized the issue was fixed. Isit helpful for you? If not would you mind sharing the sample workbook you are handling with? You can upload it via OneDrive and please don't contain private information in the workbook.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 13, 2015 10:31 AM

All replies

  • Hi

    Try this macro.

    Sub AddPict()
        Dim strFile As String, rng As Range, pic As Variant
        Set rng = Sheets("Sheet3").Range("D1")
        With Application.FileDialog(msoFileDialogOpen)
            .Filters.Add "Pictures", "*.jpg", 1
            .InitialFileName = "C:\Users\Cimjet\Pictures" 'Change this line to the proper path.
            .Show
            If .SelectedItems.Count = 0 Then Exit Sub
            strFile = .SelectedItems(1)
            pic = strFile
        End With
        Set pic = Sheets("Sheet3").Pictures.Insert(strFile)
        With pic
            .Top = rng.Top
            .Left = rng.Left
        End With
    End Sub

    You only need one Button on your worksheet to run the macro and select the picture


    Cimjet

    Sunday, March 8, 2015 9:10 PM
  • Hi Cimjet,

    Thanks for your help. I have one user form and along with pictures I would like to store other information as well. That's why I have mentioned 2 buttons on my excel sheet. One(Browse) is for selecting the image and other one(Submit) is for storing the image in Sheet3. Could you please let me know what changes do I need to make to achieve the desire result

    Thanks

    Monday, March 9, 2015 6:47 PM
  • Hi

    Did you try it ?

    The macro is using the Window "File Open" interface and I can't modify that.

    You say you're using a Userform, are the buttons on that Userform.

    Is it a Userform created with VBA or just information layout on your worksheet ?


    Cimjet


    • Marked as answer by DebChatt Tuesday, March 10, 2015 5:53 PM
    • Unmarked as answer by DebChatt Tuesday, March 10, 2015 5:53 PM
    • Edited by Cimjet Tuesday, March 10, 2015 7:53 PM
    Monday, March 9, 2015 9:59 PM
  • Yes, I did try it. I can see one single button is doing the trick. But I want One(Browse) will select the image and other one(Submit) will store the image in Sheet3. It was my mistake to call it userform. I am performing this in an excel sheet.

    Thanks

    Tuesday, March 10, 2015 5:57 PM
  • Hi

    I made a typo on my last post and made the correction. This line...

    >>The macro is using the Window "File Open" interface and I can't modify that.

    I can't modify that menu.

    Sorry


    Cimjet

    Tuesday, March 10, 2015 8:27 PM
  • Hi DebChatt,

    Yes, I did try it. I can see one single button is doing the trick. But I want One(Browse) will select the image and other one(Submit) will store the image in Sheet3. It was my mistake to call it userform. I am performing this in an excel sheet.

    Thanks

    If we can separte the procedure into two. For the "Browse" button click we can get the image path and save the value to a variable. And the "Submit" button click we can use that variable to insert the file. And here is an sample for your reference:

    Dim strFile As String
    
    Private Sub Browse_Click()
    Dim rng As Range, pic As Variant
        
         With Application.FileDialog(msoFileDialogOpen)
             .Filters.Add "Pictures", "*.jpg", 1
             .InitialFileName = "C:\Users\UserName\Pictures" 'Change this line to the proper path.
             .Show
             If .SelectedItems.Count = 0 Then Exit Sub
             strFile = .SelectedItems(1)
             pic = strFile
         End With
    End Sub
    
    Private Sub Submit_Click()
      Set rng = Sheets("Sheet3").Range("D1")
         Set pic = Sheets("Sheet3").Pictures.Insert(strFile)
         With pic
             .Top = rng.Top
             .Left = rng.Left
         End With
    End Sub
    
    Also here are some helpful links for you learning Excel developing:

    Excel 2013

    Welcome to the Visual Basic for Applications language reference for Office 2013

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 11, 2015 9:46 AM
  • Hi Fei,

    Thanks for your help. I am getting the following error while executing the macro.

    "Unable to get the insert property of the picture class" -->Line: Set pic = Sheets("Sheet3").Pictures.Insert(strFile)

    Thanks.

    Wednesday, March 11, 2015 6:27 PM
  •  Hi DebChatt,

    As far as I test, this issue only exits when the strFile is not initialized.

    After I click the browser button and the strFile is initialized the issue was fixed. Isit helpful for you? If not would you mind sharing the sample workbook you are handling with? You can upload it via OneDrive and please don't contain private information in the workbook.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 13, 2015 10:31 AM