Excel VBA Shell.Namespace返回Nothing [英] Excel VBA Shell.Namespace returns Nothing

查看:871
本文介绍了Excel VBA Shell.Namespace返回Nothing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


时间错误'91':



对象变量或未设置块变量


我通常得到这个,当我忘记使用设置与一个对象,但我已经检查了。



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

 私有函数DeCab(vSource,vDest)As Long 
objFileSest,objFileSource,objFileDest As Object
设置objShell = CreateObject(Shell.Application)
设置objFileSource = objShell.Namespace(vSource)
设置objFileDest = objShell.Namespace(vDest)
调用objFileDest.MoveHere(objFileSource.Items,4或16)'在这里失败
Decab = objFileDest.Items.Count
结束函数

Set 行中没有失​​败,但它同时设置 objFileSo url objFileDest 没有任何即使我已经确认存在vSource vDest



要确认它与.CAB文件无关,我也尝试过没有设置 objFileSource 并在设置后检查 objFileDest 的值。它仍然返回 Nothing 。为什么会这样?我在Windows 7,64位,运行Office 2010。

解决方案

您的参数必须以Variant而不是String

  Sub Tester()

Dim src,dest'<<作品
'Dim src As String,dest As String'<<给出错误你看到

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 = vs85).aspx


I'm trying to extract a .CAB file using Excel VBA, but I'm getting the following error:

Run-time error '91':

Object variable or With block variable not set

I usually get this when I forget to use Set with an Object, but I've checked for that.

All examples I can find are variations on this theme:

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

It's not failing on the Set line, but it's setting both objFileSource and objFileDest to Nothing even though I've confirmed vSource and vDest exist.

To confirm it has nothing to do with the .CAB file, I've also tried it without setting objFileSource and checking the value of objFileDest after it's set. It still returns Nothing. Why would that be? I'm on Windows 7, 64-bit, running Office 2010.

解决方案

Your parameters must be submitted as Variant, not 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

这篇关于Excel VBA Shell.Namespace返回Nothing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆