How to export attachments (images) with a given name to a folder?

前端 未结 2 1456
后悔当初
后悔当初 2021-02-10 05:02

My ex-colleague built an Access database with many record sets and each of them has one to five pictures attached. The size of the database is now really big (about 2 GB) and it

2条回答
  •  梦如初夏
    2021-02-10 05:32

    Create a new module

    From the menu:

    Create -> Module (it is in the right corner)

    Create the following function (mostly this is copy/paste from Microsoft Documentation)

        Public Function SaveAttachments(strPath As String, Optional strPattern As String = "*.*") As Long
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset2
        Dim rsA As DAO.Recordset2
        Dim fld As DAO.Field2
        Dim strFullPath As String
        
        'Get the database, recordset, and attachment field
        Set dbs = CurrentDb
    
        '
        ' MODIFY THIS LINE
        '
        Set rst = dbs.OpenRecordset("NAME_OF_THE_TABLE")
        '
        ' MODIFY THIS LINE
        '
        Set fld = rst("TABLE_FIELD_WITH_THE_ATTACHMENTS")
        
        'Navigate through the table
        Do While Not rst.EOF
        
            'Get the recordset for the Attachments field
            Set rsA = fld.Value
            
            'Save all attachments in the field
            Do While Not rsA.EOF
                If rsA("FileName") Like strPattern Then
                    strFullPath = strPath & "\" & rsA("FileName")
                    
                    'Make sure the file does not exist and save
                    If Dir(strFullPath) = "" Then
                        rsA("FileData").SaveToFile strFullPath
                    End If
                    
                    'Increment the number of files saved
                    SaveAttachments = SaveAttachments + 1
                End If
                
                'Next attachment
                rsA.MoveNext
            Loop
            rsA.Close
            
            'Next record
            rst.MoveNext
        Loop
        
        rst.Close
        dbs.Close
        
        Set fld = Nothing
        Set rsA = Nothing
        Set rst = Nothing
        Set dbs = Nothing
    End Function
    
    Sub ExportData()
      '
      ' MODIFY THIS LINE
      '
      SaveAttachments ("PATH_TO_THE_DIRECTORY_WHERE_YOU_WANT_THE_FILES_STORED")
    End Sub
    

    Then run this (F5)

提交回复
热议问题