2013-04-09 657 views
2

我正在嘗試更新VBA模塊以使用System.Windows.Forms.FolderBrowserDialog類。我宣佈我的目標如下:VBA - 用戶定義類型未定義

Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog 

運行該給我的錯誤User-defined type not defined。我想編譯器不知道那個類,所以我試着去Tools > References並增加Systems_Windows_Forms,但我仍然得到相同的錯誤。有沒有人知道我在這裏錯過了什麼?我是否也需要在代碼中引用庫?

回答

3

System.Windows.Forms.FolderBrowserDialog對我來說看起來像.Net,而不是VBA。可以在Access VBA中使用Application.FileDialog。此示例使用後期綁定,並允許用戶從瀏覽對話框中選擇一個文件夾。

Const msoFileDialogFolderPicker As Long = 4 
Dim objFileDialog As Object ' FileDialog 
Set objFileDialog = Application.FileDialog(msoFileDialogFolderPicker) 

With objFileDialog 
    .AllowMultiSelect = False 
    If .Show Then 
     Debug.Print .SelectedItems(1) 
    End If 
End With 

如果你喜歡使用早期綁定,設置爲的Microsoft Office [版本]對象庫參考。然後,您可以宣佈這樣的對象...

Dim objFileDialog As FileDialog 

而且你不需要定義常量,所以如果使用早期綁定放棄這條線......

Const msoFileDialogFolderPicker As Long = 4