SendCommand "-laydel" locks up after execution

SendCommand "-laydel" locks up after execution

skonze
Observer Observer
1,456 Views
2 Replies
Message 1 of 3

SendCommand "-laydel" locks up after execution

skonze
Observer
Observer

Using AutoCAD 2019 and VBA...

 

I have a situation where I want to delete unneeded layers.  I have created a list box where the layers can be selected and then I'm using SendCommand to run the "-laydel" command.

 

Dim Selection As String
Dim strDelete As String
For i = 0 To lbxExistLayers.ListCount - 1 'Each Selection In lbxExistLayers
If lbxExistLayers.Selected(i) = True Then
Selection = Selection & lbxExistLayers.List(i) & vbCrLf
strDelete = strDelete & "Name" & vbCr & lbxExistLayers.List(i) & vbCr
End If
Next i
Result = MsgBox("The following layer(s) AND ANY OBJECTS on them will be deleted from the drawings." & vbCrLf & vbCrLf & Selection & vbCrLf & "Are you sure you want to delete the layer(s)?", vbYesNoCancel + vbExclamation, "Warning!")
If Result = vbYes Then
ThisDrawing.SendCommand "-laydel" & vbCr & "Name" & vbCr & strDelete & vbCr & "Y" & vbCr
End If

 

The problem is that after the SendCommand has executed, AutoCAD , VBA, and the VBA form are "frozen".  I have to use the task manage to end the AutoCAD process.  However, if I put a break point on the line SendCommand line in the VBA subroutine, execution halts at that line as it should for debugging, and i can step through using the F8 key with out any issues.  The send command completes and the subroutine ends as it should without "freezing". But if I put the break point on the line after SendCommand, the "End If" line, the "freeze" will occur. 

 

Any help is appreciated.

0 Likes
Accepted solutions (1)
1,457 Views
2 Replies
Replies (2)
Message 2 of 3

Ed__Jobe
Mentor
Mentor
Accepted solution

The problem is that the SendCommand method runs asynchronously from the vba code, i.e. it runs after the vba code has completed. The reason it works in debug mode is that your forcing it to run synchronously. Use the Layers collection api instead of SendCommand.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 3

skonze
Observer
Observer

Thanks for your help!

0 Likes