我惊讶地发现,使用“Acad.Application”的通用“getObject”调用不会返回当前正在运行的Acad实例。我必须在调用中指定版本才能获得正在运行的实例。否则,将启动
acad的*第二个*实例
下面的代码适用于我-我使用了“Acad.Application.17”,这是Autocad 2008。
我还将根目录中的图形名称调暗(将DwgToOpen调暗为字符串),当然,您会希望对其进行更改
这段代码是在Excel下创建的,不是VB6,我在这里没有它,但该死的,后期绑定就是后期绑定
技巧的一部分是调用getobject,不使用路径名参数,但使用逗号:
设置acApp=getobject(,“AutoCAD.Application.17”)
,如果acad未运行,则会在没有错误陷阱的情况下出错。因此,我们首先尝试该版本的调用,如果需要,捕获错误,然后,如果必须尝试调用的“标准”版本:
设置acApp=CreateObject(“AutoCAD.Application.17”)
以在找不到正在运行的实例时创建新实例。
- 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
|