Post by rwilkinsSet App = CreateObject("AutoCAD.Application") 'Loads last used version of AutoCAD
Actually its the last version of AutoCAD the user ran. Don't use it if you
could be in a multi-install environment and want control of which version
you are firing. Below is the standard function I use - there are others but
this suits my needs. Notice that I am using late binding so there is no
reference to AutoCAD required - especially since I am not sure at trun time
which one I'll find. To use early binding, remove the Object calls and
replace with the AutoCAD specific refs commented out to the right of
Object. Also, I am tracking whether I started AutoCAD or whether it was
already running. I do this so I can shut it down if I started it.
'==== GLOBAL VARS ====
Private g_cadApp As Object 'AutoCAD AcadApplication
Private g_cadDoc As Object 'AutoCAD AcadDocument
Private g_bStartedAutoCAD As Boolean
Public Sub DoIt()
'connect to AutoCAD
g_bStartedAutoCAD = StartAutoCAD(False)
'test to see if its awake
If IsAutoCADAwake = True Then
'Do whatever.....
End If
End Sub
Public Function StartAutoCAD(blnHide As Boolean) As Boolean
'+--Fire up AutoCAD
On Error Resume Next
'Get the 2000i/2002 Application object
Set g_cadApp = GetObject(, "AutoCAD.Application.15")
'If error try connecting to 2004
If Err Then
Err.Clear
Set g_cadApp = GetObject(, "AutoCAD.Application.16")
'If error try connecting to 2005
If Err Then
Err.Clear
Set g_cadApp = GetObject("AutoCAD.Application.16.1")
'If error try starting 2000i/2002
If Err Then
Err.Clear
Set g_cadApp = CreateObject("AutoCAD.Application.15")
'If error try starting 2004
If Err Then
Err.Clear
Set g_cadApp = CreateObject("AutoCAD.Application.16")
'If error try starting 2005
If Err Then
Err.Clear
Set g_cadApp = CreateObject("AutoCAD.Application.16.1")
Do
Err.Clear
Resume
Loop While Err.Number = 429
'started AutoCAD
StartAutoCAD = True
End If
End If
Else
'found AutoCAD already running
StartACAD = False
End If
Else
'found AutoCAD already running
StartACAD = False
End If
Else
'found AutoCAD already running
StartACAD = False
End If
'Set the AutoCAD document object
Set g_cadDoc = g_cadApp.ActiveDocument
End Function
Public Function IsAutoCADAwake() As Boolean
'+-- This checks to see if AutoCAD is in a quiescent state
On Error Resume Next
Dim State As Object 'AcadState
Dim sErrMsg As String
Set State = g_cadApp.GetAcadState
If Err Then
Select Case Err.Number
Case -2147417851
Err.Clear
IsAutoCADAwake = False
Case -2147418111
Err.Clear
sErrMsg = "AutoCAD is busy...Please switch to it and" & vbCr & _
"cancel whatever command it is attempting to do."
MsgBox sErrMsg
IsAutoCADAwake = False
Case Else
sErrMsg = "Something is preventing connecting" & vbCr & _
"to AutoCAD. Please shut down and restart."
MsgBox sErrMsg
Err.Clear
Exit Function
End Select
Else
IsAutoCADAwake = IIf(State.IsQuiescent, True, False)
End If
End Function
-- Mike
___________________________
Mike Tuersley
CADalyst's CAD Clinic
Rand IMAGINiT Technologies
___________________________
the trick is to realize that there is no spoon...