【问题标题】:run time error 91 object variable not set运行时错误 91 未设置对象变量
【发布时间】:2015-03-09 08:47:37
【问题描述】:

当我排队时

oApp.Namespace(FileNameZip).CopyHere oApp.Namespace(FolderName).items

我得到一个:

运行时错误 91,未设置对象变量

zip 文件已创建并位于工作文件夹以及要压缩的文件中。我检查了我的变量,它们设置正确。

Sub Zip_genie()
    Dim FileNameZip, FolderName, oFolder
    Dim strDate As String, DefPath As String
    Dim oApp As Object

    DefPath = "c:\users\" & Environ("Username") & "\documents\Appraiser_Genie\working\"
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If

    ChDir DefPath

    FileNameZip = "report.zip"

    Set oApp = CreateObject("Shell.Application")

    'Create empty Zip File
    NewZip (FileNameZip)

    FolderName = DefPath
    Debug.Print (FolderName)
    Debug.Print (FileNameZip)
    'Copy the files to the compressed folder
    oApp.Namespace(FileNameZip).CopyHere oApp.Namespace(FolderName).items

    'Keep script waiting until Compressing is done
    On Error Resume Next
    Do Until oApp.Namespace(FileNameZip).items.count = _
        oApp.Namespace(FolderName).items.count
            Application.Wait (Now + TimeValue("0:00:01"))
    Loop
    On Error GoTo 0

    'MsgBox "You find the zipfile here: " & FileNameZip

End Sub

【问题讨论】:

    标签: variables excel object runtime-error vba


    【解决方案1】:

    尝试指定 zip 文件的完整路径名。

    例如,不仅仅是:

    FileNameZip = "report.zip"
    

    用途:

    FileNameZip = "c:\users\" & Environ("Username") & "\documents\Appraiser_Genie\target\report.zip"
    

    ...或您的 report.zip 文件所在的任何位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-07
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多