office交流網--QQ交流群號

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

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

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

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

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



    分享