Changing Layer Properties using VBA

Changing Layer Properties using VBA

revans1234
Enthusiast Enthusiast
6,796 Views
4 Replies
Message 1 of 5

Changing Layer Properties using VBA

revans1234
Enthusiast
Enthusiast

Hello,

 

I've searched for this, and have not found anything VBA, only AutoLisp.

 

I would like to change the layer property of a layer defined by its name, for a drawing. I can then put this code in a script and run it with AEUTILTIES to align the layers all my drawings in the project.

 

Specifically, I want to change the default linetype of the "LINK" layer to "DASHED" and the colour to number 9.

 

I have tried using sendcommand and the test for the -LAYER command, and it works fine until the last part. The -LAYER command remains active and I can't seem to send an escape to it. I have tried all the forum answers I can find such as Chr(27) Chr$(27) and they all give errors.

 

So I thought I would put this out there to see if anyone has some code they use that can alter layer properties that could give me some new ideas. You don't need to adapt your code to my problem if you don't want to, just for the code to show a way to solve it.

 

There are probably AutoCAD utilities etc., but I want to use VBA as I can then add these routines to others that I run for drawings already.

 

Thanks and regards to you all, I hope your day goes well.

Roger.

 

 

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

rhesusminus
Mentor
Mentor
Accepted solution

Here's a quick VBA macro that does this:

 

Option Explicit

Public Sub FixLinkLayer()

    On Error Resume Next

    ' Make sure the DASHED linetype is loaded.
    Call ActiveDocument.Linetypes.Load("DASHED", "ACAD.LIN")
    
    ' Set layer properties
    Dim lay As AcadLayer
    Set lay = ActiveDocument.Layers("LINK")
    lay.color = 9
    lay.Linetype = "DASHED"
    
End Sub

Trond Hasse Lie
EPLAN Expert and ex-AutoCAD Electrical user.
Ctrl Alt El
Please select "Accept Solution" if this post answers your question. 'Likes' won't hurt either. 😉
Message 3 of 5

revans1234
Enthusiast
Enthusiast

Hello Trond, how are you?

 

That is amazing- it works straight out of the box.

 

I found on some drawings the linetype display did not update unless I reopened the drawings, but that is not an issue with your code  and the drawings are always going to be reopened many more times yet before printing.

 

So thanks again for your quick and elegant answer.

 

Will mark as solution in a minute.

 

Best Regards,

 

Roger.

 

 

0 Likes
Message 4 of 5

chinh.trieutien
Explorer
Explorer

Why this layer.color not in object browser??

0 Likes
Message 5 of 5

rhesusminus
Mentor
Mentor
Because it's obsolete, as documented here: https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-ActiveX/files...



Trond Hasse Lie
EPLAN Expert and ex-AutoCAD Electrical user.
Ctrl Alt El
Please select "Accept Solution" if this post answers your question. 'Likes' won't hurt either. 😉