Home > Enterprise >  How do I save attachment files to a directory and keep the file extension?
How do I save attachment files to a directory and keep the file extension?

Time:11-10

I would like to save the attachments to a directory using a new file name. The following code renames the files but loose their extension. The files are image files. How do I go by?

    Dim strFileName As String
    Dim rsParent As DAO.Recordset2
    Dim rsChild As DAO.Recordset2
    Dim subField As DAO.Field2
    
    Dim strPath As String
    
    strPath = "C:\Images"
    If Len(Dir("C:\Images", vbDirectory)) = 0 Then
   MkDir "C:\Images"
   End If
        
    Set rsParent = CurrentDb.OpenRecordset("tblDonations", dbOpenSnapshot)

    With rsParent
        If .RecordCount > 0 Then .MoveFirst

        While Not .EOF
            Set rsChild = rsParent("Image").Value
        
            If rsChild.RecordCount > 0 Then rsChild.MoveFirst
            
            While Not rsChild.EOF
            Set subField = rsChild("FileData")
                            strFileName = strPath & "\" & .Fields("ItemNo")
                            If Len(Dir(strFileName)) <> 0 Then Kill strFileName

                subField.SaveToFile strFileName

                rsChild.MoveNext
            Wend

            .MoveNext
          Wend
    
    End With
subflied.Close
Set subfield = Nothing
rsChild.Close
Set rsChild = Nothing 
rsParent.Close
Set rsParent = Nothing

CodePudding user response:

Append file extension onto strFileName. Extract it from rsChild("FileType").

strFileName = strPath & "\" & .Fields("ItemNo") & "." & rsChild("FileType")
  • Related