failed to get the document object - ThisDrawing

failed to get the document object - ThisDrawing

m09366023695
Enthusiast Enthusiast
1,077 Views
4 Replies
Message 1 of 5

failed to get the document object - ThisDrawing

m09366023695
Enthusiast
Enthusiast

I want to running my initial codes such as enable menu and other things. but when I active my autocad project, I got this error:

 

Run-time error '2145320900(8021003c)':

Failed to get the document object.

 

This is my active documentation:

 

 

Private Sub AcadDocument_Activate()
'to enable menus
Call ThisDrawing.SendCommand("MENUBAR" & vbCr & "1" & vbCr)
Call addMenus

End Sub

 

 

0 Likes
Accepted solutions (1)
1,078 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

You need to provide more information about where/how this piece of code runs, which could not run on its own, because it is a "Private" subroutine.

 

Do you run it from a VBA macro in AutoCAD? Is it a part of the routine that could close the active document (thus AutoCAD could enter "no-document" state while your code still mistakenly tries to run this piece of code? Or, your code actually runs from Excel side, where there is no such thing called "ThisDrawing"?

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

m09366023695
Enthusiast
Enthusiast

this private function is a native function of AutoCAD.

 

link 

0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor
Accepted solution

OK, I see you use AcadDocument.Activate() event to run command "MenuBar" (to make the menu bar appear).

 

I also noticed that you removed some code from your original post (something related to reading data from Excel sheet?).

 

AcadDocument.Activate() event happens whenever a document becomes current active one, which is just as expected. However, it ALSO FIRES when the LAST DOCUMENT IS CLOSING, at that moment, while the ThisDrawing object is still available (e.g. not Nothing), but its functionality (properties/methods) has already become not accessible. You can verify it yourself with this code:

Option Explicit

Private Sub AcadDocument_Activate()
    
    On Error Resume Next
    
    MsgBox "Drawing name: " & ThisDrawing.Name & vbCrLf & _
    "Document count: " & Application.Documents.Count
    
    If Err.Number <> 0 Then
        MsgBox "error: " & Err.Description
    End If
    
End Sub

 

In AutoCAD, you open a couple of drawings and you would see the message box shows every time a new document is opened. Then you close each document and see the message box as well. When you close the last document, you still see the message box, but with the error message. meaning, the Activate event also fires when last drawing document closes.

 

So, I guess, in your case, once you have that Activate event handing code in place, whenever you close AutoCAD, you get VBA error, because of the reason I described.

 

From my code, as you can see, you can simply use On Error Resume Next to let last document to be closed without being stopped by the error.

 

However, I'd say doing one time work, such as showing menu bar, or loading data in this event every time is fires may not be a good choice: you do not need to run "MENUBAR" command each time a document is activated, and certainly may not need to load data from Excel each time a document is activated. You probably want to set a global/public variable as flag to indicate whether the work (showing menu bar, or load data from outside) having been done and so, you can use "If [done] then Exit" in the Activate event to bypass he work.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 5

Ed__Jobe
Mentor
Mentor

Here's a couple of simple options for showing the menubar.

1. add the sysvar to SYSVARMONITOR to alert you if it's changed

2. Add "(setvar "menubar" 1)" to your acad.lsp file. If you don't have one, simply create it with Notepad and save it to a TrustedFolders path.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes