Search code examples
vbams-accessms-access-2010public

Extract Files from an Access OLE Object


How 'bout taking those pdf-formatted package files from the table in Access and movin' 'em to a folder

If ya can, exteract the (product name) 'n the (pdf) as well That'd be awesome; lemme know if there's a fix; I'd be t.

this for example

enter image description here

This is the file: the one I created as an example. If you can, please check it and let me know the issue with this file. Also, guide me on the command needed to extract the PDF files

I used this code in VBA; Did not answer ; If another way other than coding also works; say thank you

Sub ExportPDFs()
    Dim rs As DAO.Recordset
    Dim filePath As String
    Dim fileNum As Integer
    
    
    
    Set rs = CurrentDb.OpenRecordset("SELECT pakage FROM Table1 ")
    
    If rs.RecordCount > 0 Then
        filePath = "C:\Users\AliReza\Desktop\folder\"
        
        rs.MoveFirst
        Do Until rs.EOF
            fileNum = FreeFile
            Open filePath & rs.Fields("pakage") & ".pdf" For Output As #fileNum
            Put #fileNum, , rs.Fields("Package").Value
            Close #fileNum
            
            rs.MoveNext
        Loop
    End If
    
    rs.Close
    Set rs = Nothing
End Sub

Solution

  • First, you need two different columns for the name of the document (let's call it Name) and for the OLE Object (Package) storing the document.

    Then you can use an ADODB.Stream for storing the OLE Object to a file.

    Sub ExportPDFs()
        Dim rs As DAO.Recordset
        Dim folder As String, path As String
        Dim adoStream As Object 'Late bound ADODB.Stream
        
        folder = "C:\Users\AliReza\Desktop\folder\"
        Set rs = CurrentDb.OpenRecordset("SELECT Name, Package FROM documents")
        Do Until rs.EOF
            path = folder & rs!Name & ".pdf"
            
            Set adoStream = CreateObject("ADODB.Stream")
            adoStream.Type = 1 'adTypeBinary
            adoStream.Open
            adoStream.Write rs("Package").Value
            adoStream.SaveToFile path, adSaveCreateOverWrite
            adoStream.Close
            
            rs.MoveNext
        Loop
        
        rs.Close
        Set rs = Nothing
    End Sub
    

    See: @ErikA's approach using ADODB.Stream.