运行时错误424使用VBA到AutoCAD代码

时间:2018-04-26 12:16:47

标签: excel vba

我想使用来自Excel工作表的数据,通过VBA代码在AutoCAD中绘图。

我找到了一个代码并勾选了参考文献:

  • AutoCAD Library
  • AutoCAD Object / DBX
  • AutoCAD Focus for VBA

以下是绘制圆圈的代码:

Sub AddCircle()
    ' Define the center point
    Dim ptCen(0 To 2) As Double
    ptCen(0) = 2: ptCen(1) = 3: ptCen(2) = 0#

    ' Create a Circle object in model space
    Dim circObj As AcadCircle
    Set circObj = ThisDrawing.ModelSpace.AddCircle(ptCen, 4.25)

    ThisDrawing.Application.ZoomAll
End Sub

我收到了运行时错误424.似乎我错过了一些对象。

1 个答案:

答案 0 :(得分:0)

要从Excel中在AutoCAD中绘图(必须打开),您必须添加参考工具->参考并添加[ AutoCAD 20xx类型库] 并将其插入您的代码中:

Dim app
On Error Resume Next
    Set app = GetObject(, "AutoCAD.Application")
On Error GoTo 0
If (app Is Nothing) Then Exit Sub

然后参考活动文档

Dim doc
Set doc = app.ActiveDocument

现在您可以使用 doc

添加更改为 ThisDrawing 的圆
' Create a Circle object in model space
Dim circObj As AcadCircle
Set circObj = doc.ModelSpace.AddCircle(ptCen, 4.25)

doc.Application.ZoomAll