Excel VBA Shell.Namespace返回Nothing

时间:2015-06-30 01:44:59

标签: excel vba shell excel-vba

我尝试使用Excel VBA提取.CAB文件,但我收到以下错误:

  

运行时错误' 91':

     

对象变量或未设置块变量

当我忘记将Set与对象一起使用时,我通常会这样做,但我已经检查过了。

我能找到的所有例子都是这个主题的变体:

Private Function DeCab(vSource, vDest) As Long
    Dim objShell, objFileSource, objFileDest As Object
    Set objShell = CreateObject("Shell.Application")
    Set objFileSource = objShell.Namespace(vSource)
    Set objFileDest = objShell.Namespace(vDest)
    Call objFileDest.MoveHere(objFileSource.Items, 4 Or 16) 'Fails here
    Decab = objFileDest.Items.Count
End Function

Set行没有失败,但它将objFileSourceobjFileDest设置为Nothing,即使我已经vSource行。已确认vDestobjFileSource已存在。

要确认它与.CAB文件无关,我还是在没有设置objFileDest的情况下尝试了它,并在设置后检查Nothing的值。它仍会返回.main-navigation { position: relative; padding-left: 150px; float: right; width: 100%; display: block; clear: both; text-transform: uppercase; background: #2CB3B4; height: 44px; } .main-navigation ul { list-style: none; margin: 0; padding-left: 0; } .main-navigation li { float: left; position: relative; } .main-navigation li a:after { content: ''; border: 4px solid transparent; border-top: 4px solid white; margin-left: 4px; margin-bottom: 3px; display: inline-block; margin-top: 4px; vertical-align: middle; } .main-navigation li a:only-child:after { content: ''; display: none; list-style: none; margin: 0; padding-left: 0; } .main-navigation a { display: block; padding: 0.82em 1em; font-size: 14px; text-decoration: none; line-height: 1.5em; color: #FFF; font-weight: bold; } .main-navigation ul ul { white-space: nowrap; position: absolute; left: -999em; background: #2F2F2F; z-index: 99999; padding: 0; min-width: 100%; } .main-navigation ul ul ul { left: -999em; top: 0; } .main-navigation ul ul a { } .main-navigation ul ul li { width: 100%; } .main-navigation li:hover > a, .main-navigation li.focus > a { color: #FFF; background: #2F2F2F; } .main-navigation ul ul :hover > a, .main-navigation ul ul .focus > a { } .main-navigation ul ul a:hover, .main-navigation ul ul a.focus { background: #000; } .main-navigation ul li:hover > ul, .main-navigation ul li.focus > ul { left: auto; } .main-navigation ul ul li:hover > ul, .main-navigation ul ul li.focus > ul { left: 100%; } .main-navigation .current_page_item > a, .main-navigation .current-menu-item > a, .main-navigation .current_page_item > a:hover, .main-navigation .current-menu-item > a:hover { background: #4D4D4D; } .main-navigation .current_page_ancestor > a { background: #4D4D4D; } .main-navigation ul ul .curremt_page_parent { color: green; background: #313131; } /* Small menu. */ .menu-toggle, .main-navigation.toggled ul { display: none; } @media screen and (max-width: 600px) { .menu-toggle { display: block; height: 44px; padding: 0 1em; font-weight: normal; font-size: 14px; text-decoration: none; line-height: 1.5em; color: #FFFFFF; background: #2cb3b4; border: 0; box-shadow: none; } .main-navigation.toggled .nav-menu { display: block; border-top: 1px solid #FFFFFF; padding-left: 0; position: relative; min-width: 100%; } .main-navigation.toggled { min-width: 100%; } .main-navigation.toggled a { display: block; background: #2cb3b4; min-width: 100%; } .main-navigation.toggled ul { display: block; border-top: 1px solid #FFFFFF; } .main-navigation ul { display: none; padding-left: 0; float: none; } } 。那为什么会这样?我正在运行Office 2010的Windows 7,64位上运行。

2 个答案:

答案 0 :(得分:6)

您的参数必须以Variant而非String

的形式提交
Sub Tester()

    Dim src, dest                      '<< works
    'Dim src As String, dest As String '<< gives the error you see

    src = "D:\temp\test.zip"
    dest = "D:\temp\unzip"

    DeCab src, dest

End Sub

https://msdn.microsoft.com/en-us/library/windows/desktop/bb774085(v=vs.85).aspx

答案 1 :(得分:1)

蒂姆的回答是正确的。我找到了另一种选择:

Private Function DeCab(vSource, vDest) As Long
    Dim objShell, objFileSource, objFileDest As Object
    Set objShell = CreateObject("Shell.Application")
    Set objFileSource = objShell.Namespace((vSource)) '<-extra parentheses
    Set objFileDest = objShell.Namespace((vDest)) '<-extra parentheses
    Call objFileDest.MoveHere(objFileSource.Items, 4 Or 16) 'Fails here
    Decab = objFileDest.Items.Count
End Function

将对象放在VBA中的括号中时,它将返回对象的默认值。显然,objShell.Namespace无法处理指针。它只能处理字符串文字。如果您正在传递字符串,则将签名更改为以下内容也是有效的:

Private Function DeCab(ByVal vSource, ByVal vDest) As Long