在 MS Access 中,我想用 ID 和檔案名重命名附件的檔案名,以便重復出現任何問題。例如,如果 id 是 1 并且檔案名是 ABC 那么檔案夾中的名稱應該是 1ABC 或 1_ABC 任何事情都可以。目前它保存為 ABC.extension (pdf/docx/txt)。我需要 IDfilename(1ABC.extension)。ID 是我在 Access 中的一個欄位,它是一個主鍵。
我使用的代碼如下。在我的代碼中,附件是欄位,通知是表名。
如果有人可以撰寫代碼,那將很有用。
Option Compare Database
Public Function SaveAttachments(savePath As String, Optional strPattern As String = "*.*") As Long
Dim dbs As DAO.Database
Dim rst As DAO.Recordset2
Dim rsA As DAO.Recordset2
Dim fld As DAO.Field2
Dim ID As DAO.Field2
Dim strFullPath As String
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Notices")
Set fld = rst("Attachments")
Set ID = rst("ID")
Do While Not rst.EOF
Set rsA = fld.Value
Set rsB = ID.Value
Do While Not rsA.EOF
If rsA("FileName") Like strPattern Then
strFullPath = savePath & "\" & rsB("ID") & "_" & rsA("FileName")
If Dir(strFullPath) = "" Then
rsA("FileData").SaveToFile strFullPath
End If
SaveAttachments = SaveAttachments 1
End If
rsA.MoveNext
Loop
rsA.Close
rsA.MoveNext
Loop
rst.Close
dbs.Close
Set fld = Nothing
Set ID = Nothing
Set rsA = Nothing
Set rsB = Nothing
Set rst = Nothing
Set dbs = Nothing
End Function
Private Sub Command3_Click()
SaveAttachments ("D:\Test1")
End Sub
我在分配 ID 欄位和下面的行時出錯
strFullPath = savePath & "\" & rsB("ID") & "_" & rsA("FileName")
uj5u.com熱心網友回復:
嘗試這個。
Private Sub Command0_Click()
Dim counter As Long
counter = SaveAttachments("D:\Test1")
MsgBox counter & " files exported."
End Sub
Public Function SaveAttachments(savePath As String, Optional strPattern As String = "*.*") As Long
Dim r As DAO.Recordset
Dim r2 As DAO.Recordset2
Dim strFullPath As String
Dim counter As Long
Set r = CurrentDb().OpenRecordset("Notices")
Do While Not r.EOF
Set r2 = r("Attachments").Value
Do While Not r2.EOF
If r2("FileName") Like strPattern Then
strFullPath = savePath & "\" & r("ID") & "_" & r2("FileName")
If Dir(strFullPath) = "" Then
r2("FileData").SaveToFile strFullPath
counter = counter 1
End If
End If
r2.MoveNext
Loop
If Not r2 Is Nothing Then r2.Close
r.MoveNext
Loop
If Not r Is Nothing Then r.Close
SaveAttachments = counter
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/367402.html
上一篇:如何剪輯影像以洗掉其填充
