明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2482|回复: 3

请教VBA的错误处理

[复制链接]
发表于 2004-8-19 16:19:00 | 显示全部楼层 |阅读模式
我希望第二次使用程序时不会提示“ 所命名的选择集已经存在”, 用下列代码: On Error Resume Next
Set sset = ThisDrawing.SelectionSets.Add(62)


If Err Then
Err.Clear


sset.Delete
End If
Set sset = ThisDrawing.SelectionSets.Add(62)
sset.SelectOnScreen
For Each ent In sset
If ent.EntityName = "AcDbLine" Then

pstart = ent.StartPoint
pend = ent.EndPoint
End If
Next 运行时不会提示错误, 但却不让选择图元, 直接运行这段代码以后的语句。 请各位帮忙解决。 谢谢!
 楼主| 发表于 2004-8-19 21:33:00 | 显示全部楼层
我已经找到错误的地方, 以下这样就可以了:
On Error Resume Next
Set sset = ThisDrawing.SelectionSets("62")


If Err Then
Err.Clear


sset.Delete
End If
Set sset = ThisDrawing.SelectionSets.Add("62")
sset.SelectOnScreen
发表于 2004-8-19 21:36:00 | 显示全部楼层
  1. On Error Resume Next
  2. ThisDrawing.SelectionSets("62").delete
  3. Set sset = ThisDrawing.SelectionSets.Add("62")
复制代码
发表于 2004-8-20 09:54:00 | 显示全部楼层

回复

建立一个函数: Private Function CreateSSet(ByVal name As String) As AcadSelectionSet
On Error GoTo ERR_HANDLER

Dim ssetObj As AcadSelectionSet
Dim SSetColl As AcadSelectionSets
Set SSetColl = ThisDrawing.SelectionSets

Dim index As Integer
Dim found As Boolean

found = False For index = 0 To SSetColl.Count - 1
Set ssetObj = SSetColl.Item(index)
If StrComp(ssetObj.name, name, 1) = 0 Then
found = True
Exit For 'Important.
End If
Next

If Not (found) Then
Set ssetObj = SSetColl.Add(name)
Else
ssetObj.Delete '
Set ssetObj = SSetColl.Add(name)
End If

Set CreateSSet = ssetObj

Exit Function
ERR_HANDLER:
'-----------------------------------------------
' just print the error the the debug window.
Debug.Print "Error in sub CreateSSet: " & Err.Number & " -- "; Err.Description
Resume ERR_END

ERR_END:
End Function
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-29 01:28 , Processed in 0.175650 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表