【发布时间】:2020-12-04 17:52:05
【问题描述】:
团队,我正在从 VBA 代码中提取 zip 文件,但出现错误,这是我的代码:
Sub Un_Zip_File()
Dim flname As String
Call PathCall
flname = Dir(impathn & "Transactions*.zip")
Call PathCall
Call UnZip_File(impathn, flname)
End Sub
Sub UnZip_File(strTargetPath As String, fname As Variant)
Dim oApp As Object, FSOobj As Object
Dim FileNameFolder As Variant
If Right(strTargetPath, 1) <> Application.PathSeparator Then
strTargetPath = strTargetPath & Application.PathSeparator
End If
FileNameFolder = strTargetPath
'destination folder if it does not exist
Set FSOobj = CreateObject("Scripting.FilesystemObject")
If FSOobj.FolderExists(FileNameFolder) = False Then
FSOobj.CreateFolder FileNameFolder
End If
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(fname).Items
Set oApp = Nothing
Set FSOobj = Nothing
Set FileNameFolder = Nothing
End Sub
当我运行 Un_zip_file 宏时,出现错误:
对象变量或未设置块变量
调试后继续
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(fname).Items
【问题讨论】:
-
oApp、oApp.Namespace(FileNameFolder) 或 oApp.Namespace(fname) 都可能返回错误。也许您应该在使用它们之前对其进行测试?
-
只是浏览您的代码 - 在第一个过程中您使用
flname = Dir(impathn & "Transactions*.zip")如果文件夹不存在则返回一个空路径,然后在您的第二个过程中将上面使用的路径分配给 @987654325 @ 然后检查它是否存在 - 它必须存在,否则 flname 将失败。 -
@Dharmendra,您需要添加第二个括号:
oApp.Namespace((FileNameFolder)).CopyHere oApp.Namespace((fname)).Items -
Ready-to-go Zip 和 UnZip 功能在我的项目VBA.Compress.