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
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
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 -
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.
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 -
No idea how to do either of those. I know almost nothing about VBA. Just enough to go in and fix typos basically.
Sorry.
Hi,
to check what references:
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 -
*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
You are welcome! - alfred -
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!