Vba Shell.namespace不接受字符串变量,但接受字符串本身

Vba Shell.namespace不接受字符串变量,但接受字符串本身,vba,excel,Vba,Excel,我有一个代码,在这个代码中,我想循环遍历文件夹中的文件,检查它们的内置或自定义文档属性(不打开它们),然后打开那些本来要打开的文件。 为此,我使用了Shell,并设置了使用Shell.Namespace的文件夹 我想问题出在名称空间上。当我对路径使用变量strSuborCesta时,它不起作用。当我将变量strSuborCesta打印到即时窗口中,并在Shell.Namespace(“..”)中使用打印的字符串时,它确实起作用 我说它不起作用,是指我得到: 运行时错误:91对象变量或未设置块 当

我有一个代码,在这个代码中,我想循环遍历文件夹中的文件,检查它们的内置或自定义文档属性(不打开它们),然后打开那些本来要打开的文件。 为此,我使用了
Shell
,并设置了使用
Shell.Namespace
的文件夹

我想问题出在
名称空间上。当我对路径使用变量strSuborCesta时,它不起作用。当我将变量
strSuborCesta
打印到即时窗口中,并在
Shell.Namespace(“..”)
中使用打印的字符串时,它确实起作用

我说它不起作用,是指我得到:

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

当我尝试循环遍历文件夹中的文件时(在这种情况下未设置该文件夹,因此我理解错误发生的原因,但不理解它不接受字符串变量的原因)

这条路在两方面都是正确的。但是我需要它是一个变量,而不是一个硬编码字符串

我在哪里出错?

有没有更好的方法,在不打开Excel文件的情况下检查文档属性(如注释、标题、作者等)?

下面是给我带来困难的部分(目前正处于测试阶段)

str[变量名称]
变量是字符串数据类型<代码>sFile、oShell、oDir作为变体

 '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'get a root path
strPriecinokCesta = ThisWorkbook.Path 'path to this file
strPriecinokCesta = Left(strPriecinokCesta, Len(strPriecinokCesta) - (Len(strPriecinokCesta) - InStrRev(strPriecinokCesta, "\"))) 'root path is one level above this file
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'input files are in a subfolder
strSuborCesta = strPriecinokCesta & "Zdroje\"
strSuborPripona = "Formular_BD_kotolna*.xls" 'name of a file with extension
strSuborNazov = Dir(strSuborCesta & strSuborPripona) 'actual file name
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'get access to the file system
Set oShell = CreateObject("Shell.Application")
Set oDir = oShell.Namespace(strSuborCesta) '<----this will produce an error. In contrast to using a hard coded string. Why?

For Each sFile In oDir.Items '<---- run time error 91 occurs on this line
    Debug.Print test & " : " & oDir.GetDetailsOf(sFile, 24) 'comments
Next
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'获取根路径
STRPIECINOKCESTA=此工作簿.Path'此文件的路径
STRPIECINOKCESTA=Left(STRPIECINOKCESTA,Len(STRPIECINOKCESTA)-(Len(STRPIECINOKCESTA)-InStrRev(STRPIECINOKCESTA,“\”))根路径比此文件高一级
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'输入文件位于子文件夹中
strSuborCesta=STRPIECINOKCESTA和“Zdroje”
strSuborPripona=“Formular_BD_kotolna*.xls”具有扩展名的文件的名称
strSuborNazov=Dir(strSuborCesta&strSuborPripona)'实际文件名
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'获取对文件系统的访问权限
设置oShell=CreateObject(“Shell.Application”)

Set oDir=oShell.Namespace(strSuborCesta)“变量strSuborNazov应该是一个变量

Dim strSuborNazov as variant 

oShell.Namespace(strSuborNazov) 

strSuborNazov
是否确实包含文件的完整路径?抱歉,我在那里出错,使用了不同的变量,文件名为
strSuborNazov
。正确的路径应该是文件夹路径。编辑。感谢您的评论。对于我之前的评论…
strSuborCesta
是正确的变量,应该使用它。这是包含输入文件的文件夹。