Open AutoCAD from excel with Visual Basic macro

Description :
This code below gives the way to open a new drawing AutoCAD or to get access to an opened one.
In the next messages, I'll give a better view of the possibilities of this macro
This macro was initially written for AutoCAD 2008 but you can adapt it for any other versions.

Don't forget :
References for your AutoCAD library in Visual basic editor is essential for success.
'--------------------------------------------------
Sub openautocad ()

'to add and upload autocad 2008 references (17= AutoCAD 2008; 18= AutoCAD 2010)
On Error Resume Next
With ThisWorkbook.VBProject.References
Application.DisplayAlerts = False
.AddFromFile "C:\Program Files\Common Files\Autodesk Shared\acax17en.tlb"
End With
Application.DisplayAlerts = True
On Error GoTo 0

'to declare autocad application variable
Dim AcadApp As AcadApplication

On Error Resume Next

Set AcadApp = GetObject(, "AutoCAD.Application.17")
If Err Then
Err.Clear
Set AcadApp = CreateObject("AutoCAD.Application.17")
End If

MsgBox "Now running! : " + AcadApp.Name + " version :" + AcadApp.Version

Set AcadDoc = AcadApp.Application.ActiveDocument

AcadDoc.Regen acActiveViewport
ZoomAll
AcadApp.Application.Visible = True

End Sub

No comments:

Post a Comment