27 | 06 | 2017

Run AutoCad Macro from Excel

There are situations where it can be usefull to load a AutoCad project from Excel. You don't have to set the AutoCad reference in Excel. The only thing the code does is startup AutoCad and load another Project.

Public Function RUNproject(ByVal PROJECTPATH As String, ByVal MACRONAME As String)

Dim AcadRunning As Boolean

'On Error GoTo Err_Control
AcadRunning = IsAutoCADOpen()

If AcadRunning Then
Set objAcad = GetObject(, "AutoCAD.Application")
Else
Set objAcad = CreateObject("AutoCAD.Application")
End If

objAcad.Visible = True
Set ThisDrawing = objAcad.ActiveDocument

Dim FileName As String
FileName = PROJECTPATH
' Load a sample VBA project DVB file create with AutoCAD
objAcad.LoadDVB FileName
' Run the macro
objAcad.RunMacro MACRONAME 'enter the module and routine/function name

Exit_Here:
Exit Sub
Err_Control:
MsgBox Err.Description
Resume Exit_Here
End Function

Test the function with a code that looks somewhat like this:

 

 
Sub TSTproject()
'Test the function
RUNproject "C:\Testproject.dvb", "MODULENAME.ROUTINENAME"
End Sub
Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2288651
Latest Articles