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

时间:2015-01-11 13:21:09

标签: variables excel-vba object runtime-error vba

当我到达

  

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

1 个答案:

答案 0 :(得分:0)

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

例如,而不仅仅是:

FileNameZip = "report.zip"

使用:

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

...或您的report.zip文件

的任何地方
相关问题