Home » excel » excel – export image from a userfom

excel – export image from a userfom

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a panel control which shows images in a userform generated from charts in the sheet “chart”. They depend from a combobox2 which has a “preview” button that shows the chart’s image in full quality in a secondary userform.

What I’ve been trying to do is make an export button for the user to be able to download the imnage to their PC (since the workbook is in a shared folder) by choosing the path.
BUT the issue comes in the image that I’m trying to export. I need a way to get the active picture in the image2 (that is in the second userform) so the user can download it.

Here is what I have up to now, but I get “nothing” from the Set oChart, iIneed to fill it somehow with the selected image

Function GetFolder1() As String

    Dim fldr As FileDialog
    Dim oChart As Chart
    Dim sitem As String

    Set oChart = Image2.Picture        

    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)

    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode

        GetFolder1 = .SelectedItems(1)

     oChart.Export Filename:=GetFolder1 & ("\") & ComboBox2.Text & (".bmp")
    End With

NextCode:

    Set fldr = Nothing       
End Function

(got this code from this webpage)
thnx!

How to&Answers:

Use SavePicture method. No need for Chart when you have the picture loaded in an Image control.


Public Sub SavePictureToDisk()

    Dim fldr As FileDialog

    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)

    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode

        GetFolder1 = .SelectedItems(1)

      '/ Here save the ImageControl's picture to disk.
      SavePicture UserForm2.Image2.Picture, GetFolder1 & ("\") & comboBox2.Text & ".bmp"
    End With

NextCode:

    Set fldr = Nothing

End Sub