do until error

do until error

Anonymous
Not applicable
386 Views
3 Replies
Message 1 of 4

do until error

Anonymous
Not applicable
How do I stop this with a loop?

code code and more code then

do Thisdrawing.Utility.GetEntity acEntity, Pt, "Select Entity: "

code code and more code
0 Likes
387 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
I figured it out. This works with anything you are selecting in Autocad during a loop or not.

CODE:
do
Call GetAsyncKeyState(anyInteger)
On Error Resume Next ' just for this example
' Return a point using a prompt

Thisdrawing.Utility.GetEntity acEntity, Pt, "Select Entity: "

If Err Then
If UserRightClicked Then
Exit Do
Form4.Visible = True
ElseIf GetAsyncKeyState(vbKeyEscape) <> 0 Then
'User pressed the Escape key
'End
Exit Do
Form4.Visible = True
End If
End If
loop

MODULE:
Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer

Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const VK_RBUTTON As Long = &H2&
Public Function UserRightClicked() As Boolean
If GetAsyncKeyState(VK_RBUTTON) = 1 Then UserRightClicked = True
End Function
0 Likes
Message 3 of 4

Anonymous
Not applicable
Here is a nice example without APIs
not mine though
I don't know who is an author

'' based on Tony Tanzillo's technic
'' request check "Break on Unhandled Errors" in General options
Public Sub SelectInLoop()

Dim varPt As Variant
Dim oEnt As AcadEntity
Do
On Error Resume Next
ThisDrawing.Utility.GetEntity oEnt, varPt, "Select Entity: "
If Err Then
Err.Clear
Exit Do
End If
On Error GoTo 0

If Not oEnt Is Nothing Then
'<< do whatever you need with selected entity here >>
Debug.Print oEnt.ObjectName
Set oEnt = Nothing
End If
Loop
On Error GoTo 0

End Sub

~'J'~
0 Likes
Message 4 of 4

Anonymous
Not applicable
One more thing ...

I just thought I'd add this last little bit - because some of you out there who paste this code into Acad VBA will *still* halt on an error, even with the error trap. Fatty's code is fine, and you don't need to re-install autocad, or windows, etc - it seems some installs of VBA are set with the environment option to "Break on All errors". This makes any code error handlers useless.

To correct this, in the VBA IDE, click "Tools". then "Options", select the "General" tab, and change the "Break on All errors" option to "Break on Unhandled Errors" option.
0 Likes