Excel VBA调用AutoCAD绘图遇Runtime error 424问题求助
Fixing Runtime Error 424 When Drawing a Circle in AutoCAD VBA from Excel
First off, that Runtime Error 424 ("Object Required") is popping up because ThisDrawing is an AutoCAD VBA-specific object—it doesn’t exist in Excel’s VBA environment. When you’re writing VBA from Excel to control AutoCAD, you need to explicitly connect to the AutoCAD application and its active document instead of relying on AutoCAD’s built-in objects.
Here’s how to fix your code step by step:
1. Connect to the AutoCAD Application
You need to either attach to a running AutoCAD instance or start a new one if it’s not open.
2. Replace ThisDrawing with an explicit document reference
Modify your code to grab the AutoCAD application and its active drawing first.
Here’s the corrected full code:
Sub AddCircleToAutoCAD() Dim acadApp As AcadApplication Dim acadDoc As AcadDocument Dim ptCen(0 To 2) As Double Dim circObj As AcadCircle ' Try to connect to a running AutoCAD instance On Error Resume Next Set acadApp = GetObject(, "AutoCAD.Application") On Error GoTo 0 ' If AutoCAD isn't running, start a new instance If acadApp Is Nothing Then Set acadApp = CreateObject("AutoCAD.Application") acadApp.Visible = True ' Make AutoCAD visible to verify the circle End If ' Get the active document (or create a new one if none exists) If acadApp.Documents.Count = 0 Then Set acadDoc = acadApp.Documents.Add Else Set acadDoc = acadApp.ActiveDocument End If ' Define the center point ptCen(0) = 2: ptCen(1) = 3: ptCen(2) = 0# ' Create the circle in model space Set circObj = acadDoc.ModelSpace.AddCircle(ptCen, 4.25) ' Zoom to show the circle acadApp.ZoomAll End Sub
Key Fixes Explained:
GetObject/CreateObject: These functions let Excel VBA connect to AutoCAD—either attaching to a running instance or launching a new one.acadDoc: This replacesThisDrawing; it’s the explicit reference to the active AutoCAD document your code needs to interact with.acadApp.Visible = True: Ensures AutoCAD is visible so you can confirm the circle was drawn (you can omit this if you want it to run in the background, but it’s helpful for testing).
Additional Checks:
- Double-check your VBA references: Make sure the AutoCAD libraries you selected match the version of AutoCAD you’re running (e.g., AutoCAD 2024 Library vs. 2022). Mismatched versions can also cause object errors.
- Ensure AutoCAD allows external control: Some security settings might block Excel from accessing AutoCAD—you may need to enable "Allow macros to run" in AutoCAD’s options if prompted.
内容的提问来源于stack exchange,提问作者Eslam Ahmed
相关产品推荐
相关产品推荐

