office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Access VBA調用Shell.Application出現對象不支持該動作錯誤

2017-09-06 14:29:00
zstmtony
原創
1684

Access VBA使用以下的代碼調用Shell.Application出現對象不支持該動作錯誤,錯誤號為445

使用各種方法嘗試解決,都不得要領,最后從Items的參數的數據類型入門,解決了問題


    Set   objShell   =   CreateObject( "Shell.Application ") 
        Set   objFolder   =   objShell.BrowseForFolder(0,   "select   folder ",   0,   0) 
        Set   myFolders   =   objFolder.Items() 
        Dim   i   As   Integer 
        For   i   =   1   To   myFolders.Count 
                MsgBox   myFolders.Item(i).Path 
        Next   i 

出現的錯誤提示如下:


 



原因有2個

1.i 必須 定義為Variant ,不能為 Integer ,即myFolders.Item(i) 里的參數類型必須為 Variant變量類型

2.i 是從0開始 而不是 從1 開始


修改后正常的代碼:

Private Sub Command6_Click()
    Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.BrowseForFolder(0, "select   folder ", 0, 0)
        Set myFolders = objFolder.Items()
        Dim i     As Variant
        For i = 0 To myFolders.Count - 1
                MsgBox myFolders.Item(i).Path
        Next i
End Sub 


或者直接不定義對象類型,像vbscript里一樣


Sub MyBrowseFolder() 
On Error Resume Next 
Dim objFolder As Folder 
Set objShell = CreateObject( "Shell.Application ") 
Set objFolder = objShell.BrowseForFolder(0, "select folder ", 0, 0) 
Dim i As Integer 
For i = 1 To objFolder.Items.Count 
Debug.Print objFolder.Items.Item(i).Path 
Next i 
End Sub 

其它也可以的代碼:
Set objShell = CreateObject( "Shell.Application ") 
Set objFolder = objShell.BrowseForFolder(0, "select folder ", 0, 0) 
Set myFolders = objFolder.Items() 
Set myFolder = myFolders.Item 
For Each myFolder In myFolders 
MsgBox myFolder.Path 
Next



    分享
    北京十一选五基本走势