Your Code with commentsSub InsertButton_Click()
'Personal preference but I always like to have at least one capital in my variables
'It helps me find typos
Dim insPnt As Variant
Dim blkRef As AcadBlockReference
Dim curLayer As String
curLayer = ThisDrawing.ActiveLayer.Name
'Don't need above two lines because you aren't changing the layer anymore
Dim objLayer As AcadLayer
Dim curSpace As AcadBlock
If ThisDrawing.GetVariable("CVPORT") = 1 Then
Set curSpace = ThisDrawing.PaperSpace
Else
Set curSpace = ThisDrawing.ModelSpace
End If
'''
On Error GoTo err_han
'''
For Each objLayer In ThisDrawing.Layers
If 0 = StrComp(objLayer.Name, blkLayer, vbTextCompare) Then
ThisDrawing.ActiveLayer = ThisDrawing.Layers(blkLayer)
Else: ThisDrawing.Layers.Add blkLayer
ThisDrawing.ActiveLayer = ThisDrawing.Layers(blkLayer)
End If
Next objLayer
'The above For Next section checks each layer to see if it matches blklayer
'If not it adds blklayer and sets it current but it does it as many times as there are layers
'You don't need this section anyway because you don't need to set the layer, Lets just add
'the layer in. If it exists it doesn't hurt, if not we'll make it
For Each objLayer In ThisDrawing.Layers
If objLayer.Name = blkLayer Then
objLayer.color = blkColor
objLayer.Linetype = blkLType
'you should do an Exit For here to end the loop as a general rule
End If
Next objLayer
'''you should have something here to verify the block can be loaded.
Me.hide
insPnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
If ThisDrawing.ActiveSpace = acModelSpace Then
Set blkRef = curSpace.InsertBlock(insPnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
blkRef.layer = blkLayer
Else
Set blkRef = curSpace.InsertBlock(insPnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
blkRef.layer = blkLayer
End If
'This doesn't need to be If Then anymore using curSpace
ThisDrawing.ActiveLayer = ThisDrawing.Layers(curLayer)
'You don't have to reset the layer as you didn't change it
ThisDrawing.Application.Update
Unload Me
Exit Sub
err_han:
Debug.Print Err.Number & Err.Description
Exit Sub
'Your error handling is very basic but is also servicable I would wait for a bit
'before you get deeper into error control
End Sub
And with my comments applied it comes out like Sub InsertButton_Click()
Dim insPnt As Variant
Dim blkRef As AcadBlockReference
Dim objLayer As AcadLayer
Dim curSpace As AcadBlock
Dim booLayer As Boolean
On Error GoTo err_han
If ThisDrawing.GetVariable("CVPORT") = 1 Then
Set curSpace = ThisDrawing.PaperSpace
Else
Set curSpace = ThisDrawing.ModelSpace
End If
Set objLayer = ThisDrawing.Layers.Add(blkLayer)
objLayer.color = blkColor
objLayer.Linetype = blkLType
Me.hide
insPnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
Set blkRef = curSpace.InsertBlock(insPnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
blkRef.layer = blkLayer
ThisDrawing.Application.Update
Unload Me
Exit Sub
err_han:
Debug.Print Err.Number & Err.Description
Exit Sub
End Sub
Look it over and see if you understand what I have done and more importantly, why I did it. If not, please ask away.