AutoCAD VBA打开文件对话框
利用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
效果