【发布时间】:2018-07-12 16:01:49
【问题描述】:
我正在尝试下载访问表中的所有附件并将它们存储在 Year\Month 文件夹中。我可以使用这篇文章中的指南下载它们并将它们按 ID 存储在文件夹中。
MS-Access VBA - Trying to extract each file in a table's attachments to disk?
但是,现在我尝试稍微修改一下代码,它会抛出一个错误“76”,提示找不到路径。但是在代码中,我以为我已经使用 If Len(Dir(folder, vbDirectory)) = 0 Then MkDir (folder).... 创建了文件夹。此外,当我在调试模式下将鼠标悬停在 mkdir 上时,它说:文件夹= "C:\Personal\Desktop\a\2014\11\" 这是我桌子上的前几项
有人可以帮忙吗?
该表具有年、月、附件列。 目标是将所有附件根据年份和月份以这种格式放置:“C:\Personal\Desktop\a\YEAR\MONTH\”
Sub a()
Dim database As DAO.database
Dim table As DAO.Recordset
Dim PONum As String
Dim folder As String
Set database = CurrentDb
Dim PKey As String
Dim P2Key As String
Set table = database.OpenRecordset("NIS")
With table ' For each record in table
Do Until .EOF 'exit with loop at end of table
Set Attachments = table.Fields("Attachments").Value 'get list of attachments
PKey = table.Fields("Year").Value ' get record key
P2Key = table.Fields("Month").Value
folder = "C:\Personal\Desktop\a\" & PKey & "\" & P2Key & "\" '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
End Sub
【问题讨论】: