编写代码时VBA ShellApp错误91

问题描述 投票:0回答:1

我正在编写一个函数的代码,该函数获取参数并解压缩 zip 文件。 这个函数是从另一个模块调用的,我有一个位于“Module1”内部的代码,并调用“Module2”内部的代码。 我调用此函数的方式是: Call Module2.UnzipAFile(ZipPath, UnzippedPath, File.Name) 它一直有效,直到我不得不使用 Option Explicit 并 Dim Module1 中的每个变量(必须拆分代码,因为它很长),但现在我在 ShellApp 提取文件行上收到错误 91,并且我使用了“Set” '。 我与您分享了我的整个功能以及我遇到的错误。很想听听您的修复建议。 提前致谢。

Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant, Overwrite As String)

Dim ShellApp As Object
Dim OLD_NAME As String
Dim NEW_NAME As String

Set ShellApp = CreateObject("Shell.Application")

ArchiveLastOccuranceofPoint = InStrRev(Overwrite, ".")
ArchiveFileExtension = Right(Overwrite, Len(Overwrite) - ArchiveLastOccuranceofPoint)
ArchiveFileNoExtension = Replace(Overwrite, "." + ArchiveFileExtension, "")

If Right(unzipToPath, 1) <> "\" Then
    unzipToPath = unzipToPath + "\"
End If

tempUnzip_FolderPath = unzipToPath + "tempUnzip\"

If Len(Dir(tempUnzip_FolderPath, vbDirectory)) = 0 Then           'if folder doesn't exist already
    MkDir tempUnzip_FolderPath
End If


ShellApp.Namespace(tempUnzip_FolderPath).CopyHere ShellApp.Namespace(zippedFileFullName).Items      'by A-Z it goes file by file doing an Extact to a specific folder called "tempUnzip"


'Loop through the new tempUnzip_FolderPath and if file is in the original folder then delete it
Set objFSO = New FileSystemObject
Set MySource = objFSO.GetFolder(tempUnzip_FolderPath)
Set MySourceFiles = MySource.Files

For Each File In MySourceFiles        'For Each File In mySource.SubFolders
    
    UnzippedFile_LastOccuranceofPoint = InStrRev(File.Name, ".")
    UnzippedFile_FileExtension = Right(File.Name, Len(File.Name) - UnzippedFile_LastOccuranceofPoint)
    
    If Overwrite = "Yes" Then
        If FileExists(unzipToPath + File.Name) Then
            Kill unzipToPath + File.Name
            Name File.path As unzipToPath + File.Name
        End If
    
    ElseIf Overwrite = "Ask" Then
        ShellApp.Namespace(unzipToPath).CopyHere ShellApp.Namespace(zippedFileFullName).Items
        Name File.path As unzipToPath + File.Name
    Else
        OLD_NAME = File.path
        NEW_NAME = unzipToPath & ArchiveFileNoExtension & "." & UnzippedFile_FileExtension
        
        If FileExists(NEW_NAME) Then
            Kill NEW_NAME
        End If
        
        Name OLD_NAME As NEW_NAME
        
    End If

    
Next File

Application.DisplayAlerts = False

    Dim FSO As New FileSystemObject
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    If Right(tempUnzip_FolderPath, 1) = "\" Then
        tempUnzip_FolderPath_Delete = Left(tempUnzip_FolderPath, Len(tempUnzip_FolderPath) - 1)
    Else
        tempUnzip_FolderPath_Delete = tempUnzip_FolderPath
    End If
    
    FSO.DeleteFolder tempUnzip_FolderPath_Delete, False

Application.DisplayAlerts = True

结束子

excel vba object variables set
1个回答
0
投票

我自己找到了一个有效的答案,认为它可能对其他人有帮助。

我添加了这些而不是代码中的行,它完成了工作:

'Copy the files & folders from the zip into a folder
On Error Resume Next
Set ShellApp = CreateObject("Shell.Application")
On Error GoTo 0

'Check if ShellApp is initialized before using it
If ShellApp Is Nothing Then
    MsgBox "Error creating Shell Application. Unable to proceed.", vbCritical
    Exit Sub
End If
© www.soinside.com 2019 - 2024. All rights reserved.