Launching AutoCad Issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have another program that will launch this code below (admittedly, I got it from "Through the Interface")
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
'Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Windows
Imports System
Imports Autodesk.AutoCAD.Interop
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Internal.Forms
Imports System.Windows.Forms
Public Class launchAutoCad
Public Shared Sub StartAutoCAD()
' "AutoCAD.Application.17" uses 2007, 2008, 2009
' whichever was most recently run
' "AutoCAD.Application.17.1" uses 2008, specifically
' "AutoCAD.Application.18" uses 2010, 2011, 2012
' "AutoCAD.Application.18.1" uses 2011, specifically
' "AutoCAD.Application.19" uses 2013,...
Const progID As String = "AutoCAD.Application.18"
Dim acApp As AcadApplication = Nothing
Try
acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
Catch ex2 As Exception
Try
Dim acType As Type = Type.GetTypeFromProgID(progID)
acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)
Catch ex As Exception
MessageBox.Show("Cannot create object of type """ & progID & """")
End Try
End Try
acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
If acApp IsNot Nothing Then
' By the time this is reached AutoCAD is fully
' functional and can be interacted with through code
acApp.Visible = True
acApp.ActiveDocument.SendCommand("(command " & Chr(34) & "NETLOAD" & Chr(34) & " " & _ Chr(34) & "C:\\mystuff\\myprog.dll" & Chr(34) & ") ")
End If
End Sub
End Class
At the first "Try" it will throw an exception because AutoCad is not running.
In the second "Try" we have:
acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)
Which launched AutoCad (it shows up in the task manager).
But still, it then goes into the next "Catch" and the message box says is can't create that ype of object. But I can see that it launched AutoCad.
So I copied the line:
acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
and forced it. It worked.
Why would it throw an error if it did what it was supposed to do? I see so many people using this same code on many different forums, but I can't understand why it doesn't work for me.
Thanks, in advance.

