"NO DATABASE" ERROR when automating my layers insertion in new drawing

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello everyone,
I'm coding a macro that will insert some layers whenever I create a new drawing. Because there's not such event available to the AcadDocument object I have had to create a class (MyClass) that will provide me the NewDrawing event that belongs to the AcadApplication object instead. See the code below (in case you're wondering, that MsgBox was just for testing purposes):
Option Explicit
Public WithEvents ACADApp As AcadApplication
Private Sub ACADApp_NewDrawing()
MsgBox "Hello"
Module1.AutomatedLayerInsertion
End Sub
The AutomatedLayerInsertion is a simple macro meant to insert the layers I want and which is located in Module1 of my .dvb file. Nothing fancy, it has always worked except in this case.
The thing is I have programmed everything so that each time an AcadDocument gets activated (AcadDocument_Activate event), it will trigger a module that will instantiate my MyClass class and so I will have the NewDrawing event at my disposal.
The order of events: AcadDocument_Activate: calls Init >> Init: Instantiates the MyClass class >> NewDrawing event: insertion of layers (source of the "NO DATABASE" error). Next the codes:
AcadDocument_Activate:
Private Sub AcadDocument_Activate()
Module2.Init
End Sub
Init procedure:
'Public test As New MyClass --DECLARE HERE OR TRY AS A STATIC VARIABLE
Sub Init()
Static test As New MyClass
Dim myCad As AcadApplication
Set myCad = GetObject(, "AutoCAD.Application")
Set test.ACADApp = myCad
'Alternatively do without myCad
'Set test.ACADApp = ThisDrawing.Application
End Sub
The MyClass class once again:
Option Explicit
Public WithEvents ACADApp As AcadApplication
Private Sub ACADApp_NewDrawing()
MsgBox "Hello"
Module1.AutomatedLayerInsertion
End Sub
The Source of error:
Dim myLayer As AcadLayer
Dim myColor As AcadAcCmColor
On Error GoTo myError
'Debug.Print ThisDrawing.Layers(0).Name
Set myLayer = ThisDrawing.Layers.Add("Machinery")
.
.
.
The dots are to represent that the code continues. The offensive line is the addition of the "Machinery" layer. More precisely, this is the error:
-2145386390 No database
Suspecting AutoCad couldn't find the layers Database (very confusing what that "No database" was referring to initially) I added the line that appears commented out in my code: Debug.Print ThisDrawing.Layers(0).Name. Well, if it's not commented out, that line raises an error which seems to confirm that AutoCad can't find the layers database.
Now, if I don't use the NewDrawing event and I run the macro for the layer insertion, it works like a charm. But I do want it to work as I expected it to and I can't give up! So I thought that this had something to do with starting everything from the ThisDrawing object section (remember everything starts with and ACadDocument_Activate) and maybe the ThisDrawing in my macro for the layer insertion (please, refer to the code) was conflicting in some way (just a guess, I have no idea if what I considered was a nonsense). So I changed my code to see If I could "trick" VBA and replaced ThisDrawing with a new variable that is referring to the ActiveDocument (in principle, both things are the same but I had to give it a try).
Dim myLayer As AcadLayer
Dim myColor As AcadAcCmColor
On Error GoTo myError
Dim dwg As AcadDocument
Set dwg = ActiveDocument
'Debug.Print dwg.Layers(0).Name
Set myLayer = dwg.Layers.Add("Machinery")
.
.
.
But the error persists.
I've also tried to put the layer insertion procedure inside the MyClass class, just as a desperate attempt. And it didn't work out either.
Can anybody shed some light? I've tried my best to solve the problem but I'd also like to understand what's going on. I may have made a mistake in my code or made a wrong assumption. So I hope you can help me.
I hope it doesn't bother you if I mention the two of you. You're relatively active and very knowledgeable 🙂
Thanks for getting 'til here.