Excel VBA Shell.Namespace返回Nothing [英] Excel VBA Shell.Namespace returns Nothing
问题描述
时间错误'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屋!