Object.Namespacepath错误

我写了这段代码从文件返回一些属性:

Dim strMTitle As String Dim objMshell As Object Dim objMfolder As Object Dim objMFolderItem As Object Dim strMpath As String strMpath = "C:\Users\User1\Desktop\Test4\" Set objMshell = CreateObject("shell.application") Set objMfolder = objMshell.Namespace(strMpath) Set objMFolderItem = objMfolder.ParseName("test2.xlsm") strMTitle = objMfolder.GetDetailsOf(objMFolderItem, 21) Debug.Print strMTitle 

问题是它一直返回运行时错误91 – 块variables未设置的对象variables。 最奇怪的是,当我“硬编码”objMfolderpath如下: Set objMfolder = objMshell.Namespace("C:\Users\User1\Desktop\Test4\")代码工作perferct。 我在我的macros中的多个地方使用这个path,所以我真的想“存储”在strMpath中,并使用它像这样:

 Set objMfolder = objMshell.Namespace(strMpath) 

请帮忙!

代码似乎与stringvariables一起工作,如果您使用早期绑定和Shell32.Shell如下。 此外,具有21列参数的.GetDetailsOf返回任何内容,但0返回文件名。


 Option Explicit 'Set Reference to Microsoft Shell Controls and Automation Sub dural() Dim strMTitle As String Dim objMshell As Shell32.Shell Dim objMfolder As Folder Dim objMFolderItem As FolderItem Dim strMpath As String strMpath = "C:\Users\Ron\Desktop\" Set objMshell = New Shell32.Shell Set objMfolder = objMshell.Namespace(strMpath) Set objMFolderItem = objMfolder.ParseName("20161104.csv") strMTitle = objMfolder.GetDetailsOf(objMFolderItem, 0) Debug.Print strMTitle End Sub