Hi All I am trying to save all attachments in a table to a folder. So I found this function and is saved as module one. I tested the function in the imidiate window and it works fine ?SaveAttachments("C:\Users\Shiham\Desktop\New"). I am trying to run this function from code behind a button. some thing like this for hours Call SaveAttachments ("C:\Users\Shiham\Desktop\New"). Some body please help how to call it from behind a button. Thank you
his is one of the ways I tried. Private Sub SaveAll_Enter()
Call SaveAttachments("C:\Users\Shiham\Desktop\New")
End Sub
I get the error type mismatch.
Please see the module and help.
Thank you
Code:
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
Set rst = dbs.OpenRecordset("tblAttachments")
Set fld = rst("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