Access.2007.VBA.Bibl..

(John Hannent) #1

Working with Attachments................................................................................................


Outlook has had attachments for many versions now; Access 2007 introduced the Attachment data
type for Access tables. In Outlook, attachments are a collection belonging to various item types,
primarily mail messages; Access 2007 attachments are a recordset belonging to a field of the
Attachment data type. Because both an Outlook contact item and an Access table may have attach-
ments, I needed to be able to handle copying attachments from an Outlook contact item to an
Access table and vice versa.

The Attachment field data type is new to Access 2007.

When you add a field of the Attachment data type to an Access 2007 table, it has three subfields,
which you can see in the Relationships diagram (see Figure 11.1). The attachment itself is stored in
the FileData subfield; its file name and path in the FileName subfield, and the file type in the
FileType subfield. Generally, you only need to work with the FileData and FileName subfields
when copying Access attachments.

The situation with Outlook attachments is simpler: you just save the attachment file name and
path to the Attachments collection of an item, using the Addmethod of that collection.

Most likely, you will have some attachments, either in your Outlook contacts, or in Access contact
records, so my synchronizing code needs to handle attachments. To copy attachments from one
place to another, you need to save them to files in a folder; the folder used for this purpose is
selected using the Attachments Folder Path button on the main menu, which runs an event proce-
dure that pops up a Folder Picker dialog. The procedures listed next are called from the longer
procedures that do the copying of data between the two Access compare tables, as seen on the two
contact comparison forms, or between Access and Outlook:

Public Sub CopyAccessAttsToOutlook(con As _
Outlook.ContactItem, rstSourceAttachments As _
DAO.Recordset2)
‘Called from UpdateAllOutlookContacts

On Error GoTo ErrorHandler

Set fso = CreateObject(“Scripting.FileSystemObject”)

With rstSourceAttachments
Do While Not .EOF
strDocsPath = GetOutputDocsPath

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.

NEW FEATURENEW FEATURE

Synchronizing Access and Outlook Contacts 11

Free download pdf