- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello Everyone;
I am trying to access a dwg file from excel using VBA without loading the drawing into the Autocad editor. So far I have found that I can do so using GetInterfaceObject("ObjectDBX.AxDBDocument.22"); however if Autocad is not running I get a server not running/available error.
I then fixed this by using a call to CreateObject("AutoCAD.Application.22) which starts Autocad in the background. Unfortunately, this takes a significant amount of time and I noticed - by looking at the documents property - that Autocad also opened a default drawing.
So, I am wondering if there is a way to start just Autocad's ActiveX/COM server without the rest of Autocad?
I have a fully licensed version of Autocad & Revit.
Sincerely;
Michelle
Here is my code from Excel VBA:
Sub TEST()
'Set acadApp = GetObject(, "AutoCAD.Application")
'y = acadApp.Version
Set myWS = CreateObject("WScript.Shell")
On Error Resume Next
y = myWS.RegRead("HKCR\AutoCAD.Application\CurVer\")
If Err.Number <> 0 Then
Exit Sub
End If
y = Split(y, ".", , vbTextCompare)
y = y(UBound(y))
Debug.Print "START"
On Error Resume Next
Set acadApp = GetObject(, "AutoCAD.Application." & y)
If Err.Number <> 0 Then
Flag = True
Debug.Print " WARNING: Needed to start AutoCad in background."
Debug.Print " Will close it at end of function."
Err.Clear
Set acadApp = CreateObject("AutoCAD.Application." & y)
If Err.Number <> 0 Then
Debug.Print " ERROR: Could not get AutoCad Object."
Else
acadApp.ActiveDocument.Close False
End If
End If
On Error GoTo 0
Debug.Print "OBJECT DXB"
Debug.Print ""
Set acadDb = acadApp.GetInterfaceObject("ObjectDBX.AxDBDocument." & y)
acadDb.Open ("E:\\Revit Local Files\\dwg to revit test.dwg")
If Len(acadDb.Name) = 0 Then Exit Sub
Set x = acadDb.ModelSpace
Dim Out As Range
Set Out = ActiveSheet.Range("a2")
Out = Out.Resize(x.Count, 5)
'acaddb.ModelSpace.Item(inx).GetBoundingBox (Min, Max)
For inx = 0 To x.Count - 1
Set Item = x.Item(inx)
Debug.Print inx & ": " & Item.ObjectName
Call Item.GetBoundingBox(Min, Max)
Debug.Print " min: " & Min(0) & ", " & Min(1) & " -- Max: "; Max(0) & ", " & Max(1)
Out.Item(inx + 1, 1) = inx
Out.Item(inx + 1, 2) = Min(0)
Out.Item(inx + 1, 3) = Min(1)
Out.Item(inx + 1, 4) = Max(0)
Out.Item(inx + 1, 5) = Max(1)
Next
Set acadDb = Nothing
If Flag Then
acadApp.Quit
Set acadApp = Nothing
Debug.Print "DONE"
Debug.Print ""
End If
Exit Sub
End Sub
Solved! Go to Solution.