2016-11-04 130 views
0

我寫了這個代碼從文件返回一些屬性:Object.Namespace路徑錯誤

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\")代碼工作perferct。 我用這個路徑在我的微距多的地方,所以我真的想「商店」它strMpath和使用它像這樣:

Set objMfolder = objMshell.Namespace(strMpath) 

請幫幫忙!

回答

1

如果您使用早期綁定和下面的Shell32.Shell代碼似乎與字符串變量一起工作。此外,列參數爲012的.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 

+0

參數21返回 「標題」 屬性。感謝羅恩 – Pawel

+0

@Pawel哦,好吧,我想我測試的文件沒有'Title'很高興代碼正在爲你工作。 –