Shell.namespace not accepting string variable, but accepting string itself

流过昼夜 提交于 2020-01-16 04:31:08

问题


I have a code, in which I want to loop through files in a folder, check their built-in or custom document properties (without opening them), and later open those, which are meant to be open. To do this, I'm using Shell and to set the folder I'm using Shell.Namespace.

The problem is with the Namespace, I guess. When I use a variable strSuborCesta for the path it doesn't work. When I print the variable strSuborCesta into immediate window and use the printed string inside the Shell.Namespace("....") it does work.

By it doesn't work I mean I get:

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

when I try to loop through the files in folder (which is not set in that case, so I understand why the error occurred, but don't understand why it's not accepting a string variable)

The path is correct in both ways. But I need it to be a variable, not a hardcoded string.

Where do I error?

Is there any better way, to check document properties (like comments, title, author, etc.) without opening the Excel files?

Below is the section (currently just in testing phase) that is giving me a hard time.

str[name of variable] variables are string data types. sFile, oShell, oDir are as Variants

 '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'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

回答1:


Variable strSuborNazov should be a variant

Dim strSuborNazov as variant 

oShell.Namespace(strSuborNazov) 

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



来源:https://stackoverflow.com/questions/33868233/shell-namespace-not-accepting-string-variable-but-accepting-string-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!