Layer creation code not working in AutoCAD 2013 VBA.

Layer creation code not working in AutoCAD 2013 VBA.

jpitcher8428
Enthusiast Enthusiast
4,205 Views
8 Replies
Message 1 of 9

Layer creation code not working in AutoCAD 2013 VBA.

jpitcher8428
Enthusiast
Enthusiast

I have been using a dialog box with checkboxes for creating layers for the past 5 years now with minimal problems.  I don't know much about VBA code, but when there is a problem, I can usually figure out what is going on, which is generally a typo the causes it to error.  The code was created by an employee that left the company about 3 months after I started.

 

After installing acad 2013 and the VBA enabler this code only partially works.  It will create the layer in the drawings, but will not set the color, linetype or the description.  When I click on the checkbox for any of the layer checkboxes I get a "Run-time error '13':  Type mismatch" error.  I click on the "Debug" button which opens VBA, the "This Drawing.Layers.Add" line in the below code is highlighted.

 

As a troubleshooting effort, I tried retyping the ".Color" line, when I had typed out "m_refg_pipe_n." a drop-down box appears, but "Color" is not an option anymore.  The only color option in the list is TrueColor, which we do not use.  In the next line of the below code, "Description" is an option.  So I am guessing that the ".Color" line of code is the problem.

 

Any ideas?

 

 

Code:

 

Private Sub CheckBox101_Click()
Dim m_refg_pipe_n As AcadLayer
Set m_refg_pipe_n = ThisDrawing.Layers.Add("m-refg-pipe-n")
m_refg_pipe_n.Color = acGreen

m_refg_pipe_n.Linetype = "Hidden"

m_refg_pipe_n.Description = "MN - Refrigerant Piping"
End Sub

0 Likes
Accepted solutions (1)
4,206 Views
8 Replies
Replies (8)
Message 2 of 9

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> I click on the "Debug" button which opens VBA, the "This Drawing.Layers.Add"

>> line in the below code is highlighted.

That exception will occur if the layer already exists in your drawing, then a layer with the same name can't be added again.


Try that sample:

Dim tLayer as AcadLayer
On Error Resume Next
Set tLayer = ThisDrawing.Layers.Add("NewLayerName")
if Err.Number <> 0 then
   'so the layer already exists, take it
   Set tLayer = ThisDrawing.Layers.Item("NewLayerName")
End If
On Error Goto 0  'to reset the error-handling to default

 

>> but "Color" is not an option anymore

Yes, the .Color property is marked as "obsolete" for some releases now, but it works as Autodesk holds this property for compatibility reasons.

 

- alfred -

 

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 9

jpitcher8428
Enthusiast
Enthusiast

Just to be sure, first I checked to make sure the layer was not in the drawing, and it wasn't.  Tried the process anyway, and still same situation.  

 

So then, I changed my code to add what you suggested and it still errored out, this time the ".Color" line is highlighted.

0 Likes
Message 4 of 9

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> this time the ".Color" line is highlighted

And when you check your variable m_refg_pipe_n for the layer ==> is it empty or set to the layer-object?

 

Just to make sure: have you verified that your references point to the 19.0-AutoCAD-libs?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 9

jpitcher8428
Enthusiast
Enthusiast

No idea how to do either of those.  I know almost nothing about VBA.  Just enough to go in and fix typos basically.

 

Sorry.

0 Likes
Message 6 of 9

Alfred.NESWADBA
Consultant
Consultant
Accepted solution

Hi,

 

to check what references:

  • open VBAIDE with your project
  • there is one pulldown-menu that holds an item "References" (in German the pull down is called "Extras" and I know it has another name for the english release, but I don't know what name, sorry)
  • and now check if the references marked by checkboxes are for an old version of AutoCAD or for 2013/19.0

 

To the screenshot: the 4th checked line is not necessary for some small apps, but if used, it has to point to a "19"-file!

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 7 of 9

jpitcher8428
Enthusiast
Enthusiast

*head slap*

 

As soon as I found it I remembered having to do that last year!!  That it was set for acad 2012.  I changed it to 2013 and that fixed all the problems. 

 

Thanks so much for your help, I appreciate it!

 

-Jake

0 Likes
Message 8 of 9

Alfred.NESWADBA
Consultant
Consultant

You are welcome! - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 9 of 9

Alfredo_Medina
Mentor
Mentor

Alfred, thank you very much for this help. I ran into the same problem today, trying to figure out why some VBA routines would not work properly in AutoCAD. Following your advice, I changed the reference library to work with acax19enu.tlb, and the routines started to work well again. Thank you very much!


Alfredo Medina _________________________________________________________________ ______
Licensed Architect (Florida) | Freelance Instructor | Profile on Linkedin
0 Likes