Saving Attachments to Files

The SaveAttachments procedure performs the opposite function: working with a recordset based on tblContacts, it uses a Recordset2 type recordset to iterate through the attachments collection of a record and saves each one to a file in the Output Documents folder selected on the main menu:

Public Function SaveAttachments()

On Error GoTo ErrorHandler

Dim intSpace As Integer Dim strTest As String Dim strSearch As String strDocsPath = GetOutputDocsPath()

Debug.Print "Output docs path: " & strDocsPath

Set fso = CreateObject("Scripting.FileSystemObject")

Set fld = fso.GetFolder(strDocsPath)

Set dbs = CurrentDb

Set rstTable = dbs.OpenRecordset("tblContacts") Do While Not rstTable.EOF

Create recordset of attachments for this record.

Set rstAttachments = _

rstTable.Fields("File").Value With rstAttachments Do While Not .EOF

strFileAndPath = strDocsPath _ & .Fields("FileName") Save this attachment to a file in the Output Docs folder.

Debug.Print "Saving " & strFileAndPath _ & " to " & strDocsPath & " folder"

Turn off error handler to prevent errors if the file already exists in the folder.

On Error Resume Next

.Fields("FileData").SaveToFile strFileAndPath .MoveNext Loop .Close End With rstTable.MoveNext

Loop rstTable.Close strPrompt = "All new attachments saved to " _

& strDocsPath & " folder" strTitle = "Done!"

MsgBox strPrompt, vbOKOnly + vblnformation, strTitle

ErrorHandlerExit: Exit Function


& "; Description: " & Err.Description Resume ErrorHandlerExit

End Function

7TJTT In Access 2007, it is much easier to save a database to an older format, as well as saving a copy of the database in the current format (the long awaited "Database Save As"); the three choices are right on the Save As submenu of the Office menu, as shown in Figure 9-21.


Saving a database in Access 2002-2003 format.


Saving a database in Access 2002-2003 format.

0 0

Post a comment