Click here to Skip to main content
11,813,974 members (38,306 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VB VB.NET AutoCAD
I am trying to load AutoCad from my programe. The code is:
Private Sub LoadAcad(ByVal fn As String)
      Dim acadApp As AcadApplication
      Dim acadDoc As AcadDocument
      Dim ProgId As String = "AutoCAD.Application"
         acadApp = GetObject(, ProgId)
         With acadApp
            .Visible = True
            acadDoc = .Documents.Open(fn, False)
          End With
I catch the error that AutoCad is not loaded so I load it
Catch ex1 As Exception
      acadApp = CreateObject(ProgId)
      With acadApp
         .Visible = True
         acadDoc = .Documents.Open(fn, False)
      End With
At this point AutoCad is loaded because I see a bit of it before it disappears, and it is there in the Windows Task Manager, but I don't know whether drawing "fn" was loaded. It then raises the exception "Cannot create ActiveX component."
Which is why I have captured the second error.
      Catch ex2 As Exception
         MsgBox(ex2.Message & vbCrLf & "Try Again")
      End Try
   End Try
End Sub

Now if I then execute this code again (from the form's button) it works. Except "fn" is always loaded read-only. I have checked it's attribues just before and it reports that it is Normal.

Any ideas?
Posted 10-Apr-11 4:18am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It seems to me that the first attempt locks the file, so the 2nd attempt cannot open it normally (and thus defaults to readonly mode). You are doing something wrong here. Have you gone through their documentation here:[^]
Mika Wendelius at 10-Apr-11 10:37am
Good catch, 5.
Nishant Sivakumar at 10-Apr-11 10:38am
Thanks Mika.
Stuart Nathan at 10-Apr-11 10:55am
I remarked the first .Documents.Open in the getobject successful and the drawing fn had not been loaded because acadApp = CreateObject(ProgId) raises the error.
However it is a strong possibility that Autocad has locked the file, except there is no .dwl file present.
Also, I don't understand why Acad is in Task Manager - indeed I saw the Markup Set Manager for a moment.
Stuart Nathan at 10-Apr-11 10:57am
Finally, the code you point to is for acdbmg.dll, and that assumes your inProcess. I am using Autodesk.AutoCAD.Interop.dll
Nishant Sivakumar at 10-Apr-11 10:58am
Some of the code samples were in VB.NET/C#. I suppose you could get some ideas from those.
Stuart Nathan at 10-Apr-11 11:03am
I understand, and I have been right through the manual which by the way, is nowhere near as comprehensive as the R2000 manuals, and I get no help for my problem.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You may try using GetRunningObjectTable[^] to get IRunningObjectTable[^] and see if you are able to get access to the running instance of AutoCad.

.Net version of IRunningObjectTable[^]

It might be worth a try Smile | :)

Espen Harlinn
Nishant Sivakumar at 10-Apr-11 12:40pm
Good suggestion, my 5.
Espen Harlinn at 10-Apr-11 12:41pm
Thanks Nishant :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I think the answer is that it takes a little time for the object to be made after the CreateObject statement.
So I created a timer and started it immediately after this statement.

In the timer event I use GetObject to return the Autocad object and it seems to work.

But whatever I do, the drawing is always opened as read-only.
Espen Harlinn at 10-Apr-11 17:51pm
>> But whatever I do, the drawing is always opened as read-only
This may sound stupid, but have you checked the file attributes, and you are sure you have write access to that part of the file system?
Stuart Nathan at 10-Apr-11 18:21pm
Actually yes in code using FileAttributes - it returns Normal. Also properties are NOT read-only. I thought AutoCad locked files using .dwl files, but they aren't there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Abhinav S 265
1 Maciej Los 203
2 Palash Mondal_ 190
3 Sergey Alexandrovich Kryukov 179
0 OriginalGriff 3,877
1 KrunalRohit 2,526
2 Maciej Los 2,245
3 CPallini 2,240
4 ppolymorphe 1,750

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 10 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100