I have stopped ignoring visual studio. I am having no issues accessing Com Activex objects from VB.Net with nearly the same code that works in VBA. I am not sure what all the naysaying is about. Every thread i have ever seen on running Com objects from Visual Studio says Don’t do it, It’s a Bad Idea. It seems to work fine.
as time goes by I may update this, but at this point –
Right click References in Solution Explorer, Add Reference, add the autocad type library.
To get the Autocad namespace to work across all modules, double click My Project in Solution Explorer and add Autocad to Imported Namespaces.
Get a book or study online the differences in syntax. They are pretty minor. The code editor is much improved. Arrays are a little simplified. Indexing is a little different, no static type. There is no variant type, but there is an object type that microsoft uses to read and write arrays to excel spreadsheets. (microsoft has a good one page link to connect to excel com objects HERE) Autodesk has no such single page that i can find, but here are some useful links.
All methods to be capitalized. All arguments in parentheses. No set statements.
Sometimes you have to figure out how to get the namespace shortcuts to work. The error reporting in vs really helps.
The Try, Catch, End Try error system works more cleanly.
Many more tools to use with collections and array types.
There is a stricter approach to datatypes, a setting to relax it, but i have Option Strict On. this is where a book will help. 2012 books are cheap.
So far i have pasted in VBA code, made changes per notes above, and the same objects work with the same logic – lines, polylines, dimensions, blocks, ucs, text, selection sets.
Code below is for version 2018 (“Autocad.Application.22”)
Module Module_Connect_Acad Public acadApp As AcadApplication Public acaddoc As AcadDocument Public acadms As AcadModelSpace Public Sub Connect_acad() acadApp = Nothing Dim strProgId As String = "AutoCAD.Application.22" Try '' Get a running instance of AutoCAD 'acadApp = GetObject(, strProgId) acadApp = CType(GetObject(, strProgId), AcadApplication) Catch Try '' Create a new instance of AutoCAD acadApp = CType(CreateObject(strProgId), AcadApplication) Catch ex As Exception MsgBox(ex.Message) Exit Sub End Try End Try acadApp.Visible = True '' Display the application ' MsgBox("Now running " & acadApp.Name & " version " & acadApp.Version) 'load whatever globals needed based on acadapp acaddoc = acadApp.ActiveDocument acadms = acadApp.ActiveDocument.ModelSpace End Sub End Module