利用vlax的com對象函數
'@var = FileOpenDlg("打開文件:", "D:\", "xls")
'get file dialog use VL.Application
'title@ 字符串,指定對話框的標簽。
'Default 要使用的缺省文件名;可為空字符串 ("")。
'ext 缺省的文件擴展名。如果 ext 為空字符串 (""),則缺省值為 *(所有文件類型)。如果該參數中包含 dwg 文件類型,則 getfiled 函數在對話框中顯示預覽圖像框。
Public Function FileOpenDlg(ByVal title As String, Optional ByVal defualPath As String = "", Optional ByVal extStr As String = "") As String
Dim VL As Object, VLF As Object, rtn As String
'根據AutoCAD的版本判斷使用的庫類型
If VBA.Left(Application.Version, 2) = "15" Then
Set VL = Application.GetInterfaceObject("VL.Application.1")
Else
Set VL = Application.GetInterfaceObject("VL.Application.16")
End If
Set VLF = VL.ActiveDocument.Functions.Item("GetFiled")
rtn = VLF.funcall(title, defualPath, extStr, 8)
Set VLF = Nothing: Set VL = Nothing
FileOpenDlg = rtn
End Function
Public Sub test()
MsgBox FileOpenDlg("打開文件:", "D:\", "dwg")
End Sub
效果