MS-Access VBA - 尝试将表附件中的每个文件提取到磁盘?

Joh*_*ist 6 ms-access vba

我正在尝试提取表中每条记录中包含的所有附件:

由于每个记录可能有多个附件,我想循环遍历每个记录,使用该记录的主键在磁盘上创建一个文件夹,然后将属于该记录的每个附件转储到该文件夹​​中。这是我到目前为止的代码(部分取自这里: https: //msdn.microsoft.com/en-us/library/office/ff835669.aspx),但它说我的“附件”集合不存在 EOF 。

Dim database As DAO.database
Dim table As DAO.Recordset
Dim PONum As String
Dim folder As String
Set database = CurrentDb
Set table = database.OpenRecordset("Purchasing")
With table ' For each record in table
   Do Until .EOF 'exit with loop at end of table
   Attachments = table.Fields("Attachments").Value 'get list of attachments
   PKey = table.Fields("PKey").Value ' get record key
   folder = "C:\" & PKey & "\" 'initialise folder name to create
   If Len(Dir(folder, vbDirectory)) = 0 Then ' if folder does not exist then create it
        MkDir (folder)
   End If
   '  Loop through each of the record's attachments'
   While Not Attachments.EOF 'exit while loop at end of record's attachments
        '  Save current attachment to disk in the above-defined folder.
        Attachments.Fields("FileData").SaveToFile _
              folder
        Attachments.MoveNext 'move to next attachment
   Wend
   .MoveNext 'move to next record
Loop
End With
Run Code Online (Sandbox Code Playgroud)

Joh*_*ist 4

解决了。在初始化附件对象之前,我缺少“设置”:

Set Attachments = table.Fields("Attachments").Value 'get list of attachment
Run Code Online (Sandbox Code Playgroud)