通过sendcommand向命令行输入lisp命令打开cad标准对话框:
Public Sub OpenDialog()
Dim strFileName As String
'确保 USERS1系统变盘为空
ThisDrawing.SetVariable "USERS1", ""
'显示"选择文件"对话框,并且获得用户选择的结果
ThisDrawing.SendCommand "(setvar " & """USERS1""" & "(getfiled" & """选择图形文件""" & """C:/""" & """dwg""" & "8))" & vbCr
' SendKeys "{ENTER}", True
strFileName = ThisDrawing.GetVariable("USERS1")
'显示选择结果
If Len(strFileName) = 0 Then
MsgBox "未选择任何图形文件!", vbInformation, "选择结果"
Else
MsgBox "选择的文件是:" & strFileName, vbInformation, "选择结果"
End If
End Sub
通过调用api函数打开CAD标准对话框:
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的版本判断使用的库类型
On Error Resume Next
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("选择图形文件:", "D:\", "dwg", 8) '(title, defualPath, extStr, 8)
Set VLF = Nothing: Set VL = Nothing
FileOpenDlg = rtn
If Error Then
MsgBox Err.Description
Err.Clear
End If
End Function
Public Sub test()
On Error Resume Next
MsgBox FileOpenDlg '("打开文件:", "D:\", "dwg")
End Sub