cad标准弹窗打开文件——vba代码实现

文章介绍了如何在AutoCAD环境中,通过VBA编程调用SendCommand和API函数来打开标准的图形文件选择对话框,展示了两种方法:OpenDialog子过程和FileOpenDlg函数,并给出了示例代码。
摘要由CSDN通过智能技术生成

 通过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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值