Access.2007.VBA.Bibl..

(John Hannent) #1
Public Sub CopyAccessAttsToAccess(rstSourceAttachments _
As DAO.Recordset2, rstTargetAttachments _
As DAO.Recordset2)
‘Called from CreateDenormalizedContactsTable,
‘UpdateAllAccessContacts, UpdateOutlookContactID,
‘UpdateAccessContactID, UpdateOutlookContactName,
‘UpdateAccessContactName, UpdateAllAccessContacts,
‘UpdateOutlookContactID, cboAttachments_Click on
‘fsubCopyFieldData

On Error GoTo ErrorHandler

Set fso = CreateObject(“Scripting.FileSystemObject”)

Do While Not rstSourceAttachments.EOF

Need to extract the file name from the FileName field, using the SplitFileName function, because it
sometimes contains the path (sometimes multiple times) as well as the file name.

strFile = _

SplitFileName(rstSourceAttachments.Fields(“FileName”))
Debug.Print “File name: “ & strFile
strFileAndPath = strDocsPath & strFile
Debug.Print “File and path: “ & strFileAndPath

On Error Resume Next

Check whether this file already exists in the folder, and save it to the folder if not.

Set fil = fso.GetFile(strFileAndPath)
If fil Is Nothing Then

Save this attachment to a file in the Output Docs folder.

rstSourceAttachments.Fields(“FileData”).SaveToFile _
strFileAndPath
Debug.Print “Saving “ & strFileAndPath
End If

Load this attachment to the Attachments field of the target table.

rstTargetAttachments.AddNew
rstTargetAttachments.Fields(“FileData”).LoadFromFile _
(strFileAndPath)
rstTargetAttachments.Update
Kill strFileAndPath
rstSourceAttachments.MoveNext

Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf