AutoCAD Mechanical Forum
Welcome to Autodesk’s AutoCAD Mechanical Forums. Share your knowledge, ask questions, and explore popular AutoCAD Mechanical topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create new layer in AutoCad using excel VBA

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Anonymous
7947 Views, 4 Replies

Create new layer in AutoCad using excel VBA

Anonymous
Not applicable

Hello to everyone,

I have a problem creating a new layer from excel VBA.

This is the code. Last if statements is wrong.

 

Sub New_Layer()

Dim acadApp As AcadApplication
Dim acadDoc As AcadDocument

Dim newLayer As AcadLayer
Dim layerName As String


'Check if AutoCAD is open.
On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0
    

    'If AutoCAD is not opened create a new instance and make it visible.
If acadApp Is Nothing Then
    Set acadApp = New AcadApplication
    acadApp.Visible = True
End If

    'Check if there is an active drawing.
On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
On Error GoTo 0

    'No active drawing found. Create a new one.
If acadDoc Is Nothing Then
    Set acadDoc = acadApp.Documents.Add
    acadApp.Visible = True
End If

'check to see if layer already exists and if not make a new

On Error Resume Next
    layerName = "Layer_1"
    Set newLayer = ThisDrawing.Layers.Add(layerName)
    newLayer.Color = acBlue
On Error GoTo 0

If newLayer Is Nothing Then
    Set newLayer = ThisDrawing.Layers.Add(layerName) 'Here I have error Object required
End If




End Sub

If someone can help me I will be very appreciative.

0 Likes

Create new layer in AutoCad using excel VBA

Hello to everyone,

I have a problem creating a new layer from excel VBA.

This is the code. Last if statements is wrong.

 

Sub New_Layer()

Dim acadApp As AcadApplication
Dim acadDoc As AcadDocument

Dim newLayer As AcadLayer
Dim layerName As String


'Check if AutoCAD is open.
On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0
    

    'If AutoCAD is not opened create a new instance and make it visible.
If acadApp Is Nothing Then
    Set acadApp = New AcadApplication
    acadApp.Visible = True
End If

    'Check if there is an active drawing.
On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
On Error GoTo 0

    'No active drawing found. Create a new one.
If acadDoc Is Nothing Then
    Set acadDoc = acadApp.Documents.Add
    acadApp.Visible = True
End If

'check to see if layer already exists and if not make a new

On Error Resume Next
    layerName = "Layer_1"
    Set newLayer = ThisDrawing.Layers.Add(layerName)
    newLayer.Color = acBlue
On Error GoTo 0

If newLayer Is Nothing Then
    Set newLayer = ThisDrawing.Layers.Add(layerName) 'Here I have error Object required
End If




End Sub

If someone can help me I will be very appreciative.

4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Anonymous

Anonymous
Not applicable
Accepted solution

Solved!!!

Sub New_Layer()

Dim acadApp As AcadApplication
Dim acadDoc As AcadDocument

Dim newLayer As AcadLayer
Dim layerName As String


'Check if AutoCAD is open.
On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0
    

    'If AutoCAD is not opened create a new instance and make it visible.
If acadApp Is Nothing Then
    Set acadApp = New AcadApplication
    acadApp.Visible = True
End If

    'Check if there is an active drawing.
On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
On Error GoTo 0

    'No active drawing found. Create a new one.
If acadDoc Is Nothing Then
    Set acadDoc = acadApp.Documents.Add
    acadApp.Visible = True
End If

'check to see if layer already exists and if not make a new

On Error Resume Next
    layerName = "Layer_1"
    Set newLayer = acadDoc.Layers.Add(layerName)
    newLayer.Color = acBlue
On Error GoTo 0

If newLayer Is Nothing Then
    Set newLayer = acadDoc.Layers.Add(layerName)
End If
acadDoc.ActiveLayer = newLayer



End Sub

Solved!!!

Sub New_Layer()

Dim acadApp As AcadApplication
Dim acadDoc As AcadDocument

Dim newLayer As AcadLayer
Dim layerName As String


'Check if AutoCAD is open.
On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0
    

    'If AutoCAD is not opened create a new instance and make it visible.
If acadApp Is Nothing Then
    Set acadApp = New AcadApplication
    acadApp.Visible = True
End If

    'Check if there is an active drawing.
On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
On Error GoTo 0

    'No active drawing found. Create a new one.
If acadDoc Is Nothing Then
    Set acadDoc = acadApp.Documents.Add
    acadApp.Visible = True
End If

'check to see if layer already exists and if not make a new

On Error Resume Next
    layerName = "Layer_1"
    Set newLayer = acadDoc.Layers.Add(layerName)
    newLayer.Color = acBlue
On Error GoTo 0

If newLayer Is Nothing Then
    Set newLayer = acadDoc.Layers.Add(layerName)
End If
acadDoc.ActiveLayer = newLayer



End Sub
Message 3 of 5
cheryl.buck
in reply to: Anonymous

cheryl.buck
Autodesk Support
Autodesk Support

Hi @Anonymous,

 

Thank you for returning and adding a post with the Solution.   

 

Please select the Accept as Solution button on your post so other members of the community can more easily find the solution.

 

All the best.

Cheryl Buck
Technical Support Specialist



Did a post answer your question or help resolve the issue? Please click the Accept Solution button.
If you find a response helpful, consider Liking the post.

0 Likes

Hi @Anonymous,

 

Thank you for returning and adding a post with the Solution.   

 

Please select the Accept as Solution button on your post so other members of the community can more easily find the solution.

 

All the best.

Cheryl Buck
Technical Support Specialist



Did a post answer your question or help resolve the issue? Please click the Accept Solution button.
If you find a response helpful, consider Liking the post.

Message 4 of 5
ahmedmontaser504
in reply to: Anonymous

ahmedmontaser504
Community Visitor
Community Visitor
I want acode that make layer isolate to active layer
0 Likes

I want acode that make layer isolate to active layer
Message 5 of 5
pendean
in reply to: ahmedmontaser504

pendean
Community Legend
Community Legend
0 Likes

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report