提问人:Alireza Daneshmayeh 提问时间:8/23/2023 最后编辑:Alireza Daneshmayeh 更新时间:8/24/2023 访问量:50
如何在VBA Access软件中显示未提取为输出的文件的名称
How to display the names of files that are not extracted as output in VBA Access software
问:
Sub ExportPDFs()
Dim rs As DAO.Recordset
Dim folder As String, path As String
Dim adoStream As Object 'Late bound ADODB.Stream
Dim failedFiles As New Collection 'list of failed files
Dim failedFileNames As String 'A string to hold the names of failed files
folder = "F:\rkp\archive packing\tarhebasteh\"
Set rs = CurrentDb.OpenRecordset("SELECT namemotor , FILE FROM tarhebasteh")
failedFileNames = "" 'Definition and initialization
Do Until rs.EOF
path = folder & rs!namemotor & ".pdf"
Set adoStream = CreateObject("ADODB.Stream")
adoStream.Type = 1 'adTypeBinary
adoStream.Open
adoStream.Write rs("FILE").Value
On Error Resume Next
adoStream.SaveToFile path, adSaveCreateOverWrite
If Err.Number <> 0 Then 'If an error occurs
failedFiles.Add rs!namemotor ' Add filename to failure list
End If
On Error GoTo 0
adoStream.Close
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
'Show failed files
If failedFiles.Count > 0 Then
Dim failedFile As Variant
For Each failedFile In failedFiles
failedFileNames = failedFileNames & vbCrLf & failedFile
Next failedFile
MsgBox "The following files were not output:" & vbCrLf & vbCrLf & failedFileNames
Else
MsgBox "All files have been moved to the output."
End If
End Sub
从此行获取错误failedFileNames = failedFileNames & vbCrLf & failedFile
这个 erore 是object invalid or no longer set
你能帮我把代码做得更好吗? 或者使用其他代码来执行此操作
前面的代码适用于将 500 个文件提取为 PDF 并在输出中显示 300 个文件。但是,您上面提供的代码专门提取了 480 个 PDF 文件并将它们显示为输出。
Sub ExportPDFs()
Dim rs As DAO.Recordset
Dim folder As String, path As String
Dim adoStream As Object 'Late bound ADODB.Stream
folder = "C:\Users\AliReza\Desktop\folder\"
Set rs = CurrentDb.OpenRecordset("SELECT Name, Package FROM documents")
Do Until rs.EOF
path = folder & rs!Name & ".pdf"
Set adoStream = CreateObject("ADODB.Stream")
adoStream.Type = 1 'adTypeBinary
adoStream.Open
adoStream.Write rs("Package").Value
adoStream.SaveToFile path, adSaveCreateOverWrite
adoStream.Close
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub
答:
1赞
Tim Williams
8/24/2023
#1
failedFiles.Add rs!namemotor
将 Field 对象添加到集合中,以便在关闭不再可用的记录集后。
failedFiles.Add rs!namemotor.Value
将改为添加字段内容,以便您可以在关闭 Recordset 后使用它。
评论
0赞
Alireza Daneshmayeh
8/24/2023
谢谢;太完美了,我没有使用白色 vba 编程语言,错误取自另一行,我问 chatgbt ,没有意识到它来自你说的那行_@Tim威廉姆斯
评论