You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加Option Explicit后出现Error 91:创建Shell Application失败求助

解决Error 91:对象变量或With块变量未设置问题

问题原因分析

Error 91出现在ShellApp.Namespace(tempUnzip_FolderPath).CopyHere这一行,核心原因是ShellApp.Namespace()返回了Nothing,导致无法调用后续方法。具体诱因包括:

  • 函数内大量变量未声明,开启Option Explicit后可能因变量值异常导致路径无效
  • 调用函数时参数传递错误,第三个参数应为覆盖选项(如"Yes"/"No"/"Ask"),却传入了文件名File.Name
  • 路径拼接使用+易引发类型错误,且存在多余反斜杠的问题

分步修复方案

1. 修正函数内的变量声明与路径处理

在Module2的UnzipAFile函数顶部添加Option Explicit,并声明所有未定义的变量,同时修复路径拼接逻辑:

Option Explicit

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
    Dim ArchiveLastOccuranceofPoint As Integer
    Dim ArchiveFileExtension As String
    Dim ArchiveFileNoExtension As String
    Dim tempUnzip_FolderPath As String
    Dim objFSO As FileSystemObject
    Dim MySource As Folder
    Dim MySourceFiles As Files
    Dim File As File
    Dim UnzippedFile_LastOccuranceofPoint As Integer
    Dim UnzippedFile_FileExtension As String
    Dim tempUnzip_FolderPath_Delete As String
    Dim FSO As FileSystemObject
    
    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
        MkDir tempUnzip_FolderPath
    End If
    
    ' 添加错误检查,确保Namespace对象有效
    If ShellApp.Namespace(zippedFileFullName) Is Nothing Then
        MsgBox "压缩文件路径无效:" & zippedFileFullName, vbCritical
        Exit Sub
    End If
    If ShellApp.Namespace(tempUnzip_FolderPath) Is Nothing Then
        MsgBox "临时解压文件夹路径无效:" & tempUnzip_FolderPath, vbCritical
        Exit Sub
    End If
    
    ShellApp.Namespace(tempUnzip_FolderPath).CopyHere ShellApp.Namespace(zippedFileFullName).Items
    
    ' 后续代码保持不变,仅变量已声明
    Set objFSO = New FileSystemObject
    Set MySource = objFSO.GetFolder(tempUnzip_FolderPath)
    Set MySourceFiles = MySource.Files
    
    For Each File In MySourceFiles
        
        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
    
    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

End Sub

2. 修正Module1中的函数调用参数

原调用将第三个参数传入File.Name,但函数第三个参数应为覆盖模式("Yes"/"No"/"Ask"),需根据实际需求修改:

' 示例:以覆盖现有文件的模式调用
Call Module2.UnzipAFile(ZipPath, UnzippedPath, "Yes")

注意:确保ZipPath是完整的ZIP文件路径(如C:\Files\test.zip),而非仅文件夹路径。

3. 辅助检查

若未定义FileExists函数,需补充实现:

Function FileExists(filePath As String) As Boolean
    FileExists = (Len(Dir(filePath)) > 0)
End Function

内容的提问来源于stack exchange,提问作者Kobe2424

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 00:40:17