使用从Excel运行的VBA来抓取/打开AutoCAD并开始绘制新的绘图。
到目前为止,我所尝试的内容取自我在网络的各个部分找到的内容:
Sub Open_AutoCad(NewFile As AcadDocument, ACAD As AcadApplication)
'Dim MyAcadPath As String
'Dim bReadOnly As Boolean
On Error Resume Next
Set ACAD = GetObject(, "autocad.Application")
If (Err <> 0) Then
Err.Clear
Set ACAD = CreateObject("autocad.Application")
If (Err <> 0) Then
MsgBox "Could Not Load AutoCAD!", vbExclamation
End
End If
'*****************************************
Set NewFile = ACAD.Documents.active <-need correct syntax for active document
'*****************************************
Else
Set NewFile = ACAD.Documents.Add
End If
ACAD.Visible = True
' PREVIOUS OPEN METHOD THAT IS HARD CODED FILE PATH
' MyAcadPath = "c:\Temp\Blank Drawing.dwg"
' bReadOnly = True
' Set NewFile = ACAD.Documents.Open(MyAcadPath, bReadOnly)
'
' If (NewFile Is Nothing) Then
'ErrorMessage:
' If NewFile = "False" Then End
' MsgBox "Could not find the required file that should be located at" & vbCr & MyAcadPath & vbCr & "Please rename or relocated the specified file as needed."
' End
' End If
'close AutoCAD Process
'ACAD.Quit
'Set ACAD = Nothing
'Set NewFile = Nothing
End Sub
我最初是从硬编码路径打开一个绘图,但这不是我想要发生的。我想要获取恰好在autocad中设置的默认模板文件并使用它。以为它可以节省必须确保所有用户都拥有硬编码路径。
我选择使用ACAD.Documents.Add方法添加默认模板文件,效果很好。问题是AutoCAD在打开特定文件时(至少在我的系统上)自动创建Drawing1。因此,当我点击.Add时,我会结束Drawing2,并且所有工作都将在drawing2中执行。所以为了避免这种情况,我只使用.Add方法,如果AutoCAD已经存在/正在运行。这方面工作正常。当AutoCAD作为其打开例程的一部分创建时,如何将Drawing1分配给变量Newfile?
Sub Open_AutoCad(NewFile As AcadDocument, ACAD As AcadApplication)
'Dim MyAcadPath As String
'Dim bReadOnly As Boolean
On Error Resume Next
Set ACAD = GetObject(, "autocad.Application")
On Error Goto 0
If ACAD=Nothing Then
Err.Clear
Set ACAD = CreateObject("autocad.Application")
If (Err <> 0) Then
MsgBox "Could Not Load AutoCAD!", vbExclamation
Exit Sub
End If
'*****************************************
Set NewFile = ACAD.Documents.activedocument
'*****************************************
Else
Set NewFile = ACAD.Documents.Add
End If
ACAD.Visible = True
End Sub