VBA Automation Fails to Connect with Civil 3D 2023 / AutoCAD 2023

VBA Automation Fails to Connect with Civil 3D 2023 / AutoCAD 2023

FBritoG
Explorer Explorer
253 Views
1 Reply
Message 1 of 2

VBA Automation Fails to Connect with Civil 3D 2023 / AutoCAD 2023

FBritoG
Explorer
Explorer

 

Hello everyone,

 

I'm facing a persistent issue when trying to automate the creation of longitudinal profiles in Civil 3D 2023 (and AutoCAD 2023) from an Excel VBA macro.

 

The goal is to read data from Excel and draw elements such as terrain lines, pipes, manholes, dimensions, and text directly into the model space. However, I cannot establish a reliable connection between Excel and Civil 3D/AutoCAD using GetObject or CreateObject, despite having a valid license and a clean installation.

 

What I've Tried:

 

Basic connection with GetObject:

vba

Set acadApp = GetObject(, "AutoCAD.Application")

Sometimes works, but most of the time it fails without a clear reason.

 

Using the specific Civil 3D name:

vba

Set acadApp = GetObject(, "Civil3D.Application")

Never works, even when Civil 3D is open.

 

Running accoreconsole.exe /regserver:

  • Executed as Administrator.
  • Full repair of Civil 3D 2023 via Autodesk Desktop App.
  • The COM registration seems not to be restored correctly.

 

VBA References:

Added "Autodesk Civil 3D 2023 Type Library".

The problem persists even with the reference enabled.

 

Alternative: .SCR scripts:

They work, but this is not the ideal solution as I need an interactive workflow from Excel.

Running as Administrator, macros enabled, only one AutoCAD process open, etc.

 

Symptoms:

The code stops at Set acadDoc = acadApp.ActiveDocument with the error "Object variable or With block variable not set".

Sometimes, acadApp is Nothing even when Civil 3D is open.

The system does not recognize Civil3D.Application in the COM registry.

No license or installation errors.

 

Environment:

Operating System: Windows 10/11 (64-bit)

Applications: Civil 3D 2023 (valid license), AutoCAD 2023

Automation Tool: Excel 2019/365 with VBA

Goal: Draw longitudinal profiles from structured data.

 

Question:

Has anyone successfully established a stable and reliable connection between VBA in Excel and Civil 3D 2023?

Is there a definitive solution for GetObject to work consistently?

Is this a known issue in version 2023?

 

I would greatly appreciate any guidance, working example, or viable alternative.

 

Best regards.

0 Likes
254 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor

While the description of your issue looks quite detailed, there are still some things that are not very clear/accurate:

 

1. You did not mention that when GetObject(,"AutoCAD.Application") is called, is that ALWAYs a running AutoCAD session? If not, the call would not succeed, of course. If when you run your Macro in Excel, and an AutoCAD session may or may not be running, the usual approach is to call GetObject() first and trap a possible error (On Error Resume, usually) due to the possible AutoCAD session not available. when this error occurs, call CreateObject() to start a new AutoCAD session. Since you did not show more code, it is not clear if you have handled this situation, or handled it correctly;

 

2. Of course, you cannot use GetObject(, "Civil.Application"): there is not class called "Civil.Application" in Windows registry.

 

3. What "accoreconsole.exe" has anything to do with running VBA code? Why did you mention it? Also, what is "regserver"? Are you talking "RegSvr32.exe" that is used to register COM component? In your case, you should not use it at all.

 

4. When you say "...Added "Autodesk Civil 3D 2023 Type Library"...", it is completely unclear or wrong: there is nothing called "Autodesk Civil 3D 2023 Type Library". I assume you are talking adding references to AutoCAD/C3D COM objects in Excel VBA project. IN this case, you need to add "AutoCAD 2021 Type Library" (YES, it is AutoCAD 2021 Type Library!), and the C3D reference "Autodesk.Civil Engineering 13.x Land Object Library", and very likely also "Autodesk Civil Engineering 13.x  UI Land Object Library".

 

5. When you say "... The code stops at Set acadDoc = acadApp.ActiveDocument with the error "Object variable or With block variable not set"..." does your code make sure the running AutoCAD session has a draw (exiting or new empty one) open while this line of code executes? It looks like, there is no drawing open (thus no ActiveDocument). BUt since you did not show your code in context, nothing more I can say.

 

Extra thoughts:

 

1. If I have to use VBA in this case, I'd prefer to run from AutoCAD/C3D side's VBA.

2. Even you fixed the current issue and made it work, unless you do not see any future enhancement/feature expansion required, I would stop waste time on VBA and use AutoCAD/C3D .NET API.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes