Forum Discussion

Marco_DellOca's avatar
Marco_DellOca
Copper Contributor
Apr 29, 2021

extract image from Ms Excel using Ms Access Vba

Using MS Access and Vba, I need to extract images contained in an excel file, sheet n. 2 and save them in a directory on the pc. Where can I find information? Thanks in advance Marco Dell'Oca
  • Marco_DellOca's avatar
    Marco_DellOca
    Jun 10, 2021

    Woldman

    thanks Tieme,

    I solved the problem by reading the Excel file from Access in this way:

     

     

        Dim XL As Excel.Application
        Dim WB As Excel.Workbook
        Dim WS As Excel.Worksheet
        Dim SH As Excel.Worksheet
        
        Set XL = CreateObject("Excel.Application")
        Set WB = XL.Workbooks.Open(<file Excel path>)
        XL.Visible = False
        WB.Worksheets(sheet_no + 1).Select
        Set WS = WB.ActiveSheet
        Set SH = WB.Worksheets.Item(sheet_no + 1)
    
        Dim output_folder As String
        Dim image As Object, image_name As String, img_dimension As Variant, img_area As Variant
        
        output_folder = CurrentProject.Path & "\Temp\"
    
        For Each image In ActiveSheet.Shapes
            image_name = image.TopLeftCell.Row & "_" & image.TopLeftCell.Column
     
            image.Select
            With Selection.ShapeRange
                .Fill.Visible = msoFalse
                .Line.Visible = msoFalse
                .Rotation = 0#
                .ScaleHeight 1#, msoTrue, msoScaleFromTopLeft
                .ScaleWidth 1#, msoTrue, msoScaleFromTopLeft
            End With
            With Selection.ShapeRange.pictureFormat
                .CropLeft = 0#
                .CropRight = 0#
                .CropBottom = 0#
                .CropTop = 0#
                .TransparentBackground = msoFalse
            End With
            Selection.CopyPicture
            Set img_dimension = ActiveSheet.ChartObjects.Add(0, 0, image.Width, image.Height)
            Set img_area = img_dimension.Chart
            img_dimension.Activate
            With img_area
                .ChartArea.Select
                .Paste
                .Export (output_folder & "\" & image_name & ".jpg")
            End With
            img_dimension.Delete
            
    successivo:
        Next
        
        Set XL = Nothing
        Set WB = Nothing
        Set WS = Nothing
        Set SH = Nothing
        
    Exit Sub
        
    err_ExportMyPicture:
        MsgBox "the immage: " & image_name & " was not exported."
        
        
    Resume successivo

     

     

     

    Thanks a lot for the tip Marco Dell'Oca  

Resources