gpt4 book ai didi

Excel VBA Shell.Namespace 返回 Nothing

转载 作者:行者123 更新时间:2023-12-04 16:16:48 25 4
gpt4 key购买 nike

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

Run-time error '91': Object variable or With block variable not set

当我忘记对对象使用 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 即使我已确认 vSourcevDest 存在。

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

最佳答案

您的参数必须作为 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

关于Excel VBA Shell.Namespace 返回 Nothing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31128248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com