Object.Namespace路径错误

时间:2016-11-04 09:52:40

标签: excel vba

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

 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 - 没有设置块变量的对象变量。最奇怪的是当我用这样的路径“硬编码”objMfolder时: Set objMfolder = objMshell.Namespace("C:\Users\User1\Desktop\Test4\")代码工作正常。 我在宏的多个位置使用此路径,所以我真的想将它“存储”在strMpath中并像这样使用它:

Set objMfolder = objMshell.Namespace(strMpath) 

请帮忙!

1 个答案:

答案 0 :(得分:1)

如果使用早期绑定,则代码似乎与字符串变量一起使用,如下所示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