I was surprised to see that the generic "getObject" call, using "Acad.Application", would NOT return the current running instance of acad. I had to specify the version in the call to get the running instance. Otherwise, a *second* instance of
acad was started.
The code below works for me - I used "Acad.Application.17", which is Autocad 2008.
I also dimmed the name of a drawing in my root dir (Dim DwgToOpen As String), which you will want to change , of course.
This code was created under Excel, not VB6, which I dont have handy here in work, but hell, late binding is late binding.
Part of the trick is to call getobject without the Pathname parameter , but WITH a comma:
Set acApp = GetObject(, "AutoCAD.Application.17")
which errors out without an error trap, if acad is not running. So we try that version of the call first, trap the error if needed, and then, if we have to try the 'standard' version of the call:
Set acApp = CreateObject("AutoCAD.Application.17")
to create a new instance if we cant find a running instance.
Sub Main()
Dim acApp As Object
' make sure under "Tools|Options|General" you select "Break on Unhandled Errors" selected
' Dim a Dwg Name to open in acad
Dim DwgToOpen As String: DwgToOpen = "c:\test.dwg"
On Error Resume Next ' call errors out if Acad 2008 not running
' get running instance of AutoCad
Set acApp = GetObject(, "AutoCAD.Application.17")
On Error GoTo 0
' if we dont have it, create from scratch
If acApp Is Nothing Then ' start new session of Autocad
Set acApp = CreateObject("AutoCAD.Application.17")
If acApp Is Nothing Then
MsgBox "Unable to open Autocad 2008"
Set acApp = Nothing
Exit Sub
Else
MsgBox "We are running a new instance of acad 2008"
End If
Else
MsgBox "We are using the current running Aacad 2008"
End If
' well, we have an instance of acad ruinning, whether an already running version,
' or a new version. Open the drawing in acad, as read-only
Dim MyDwg As Object
Set MyDwg = acApp.Documents.Open(DwgToOpen, True)
End Sub