Run-time error help

Discussion in 'AutoCAD' started by Tim Riley, Aug 20, 2003.

  1. Tim Riley

    Tim Riley Guest

    Every once in a while users get a run-time error in my code. I am unable
    to recreate this error however. If anyone could tell me what I'm doing wrong
    it would be greatly appricicated.


    Thanks,
    Tim Riley



    ************************code****************************

    Option Explicit
    Public Sub AcadStartUp()

    Dim SelSet As AcadSelectionSet
    Dim AT As AcadAttribute
    Dim FilterType(0 To 0) As Integer
    Dim FilterData(0 To 0) As Variant
    Dim FixLay As AcadLayer

    FilterType(0) = 0
    FilterData(0) = "ATTDEF"

    On Error Resume Next
    ThisDrawing.SelectionSets.Add "SelSet"
    Set SelSet = ThisDrawing.SelectionSets("SelSet")
    SelSet.Clear
    On Error GoTo 0

    SelSet.Select acSelectionSetAll, , , FilterType, FilterData

    If SelSet.Count <> 0 Then
    MsgBox "You Have " & SelSet.Count & " Attribute Definition(s) in " &
    ThisDrawing.Name & ". Please Change Them To Text."
    'Create a new layer
    Set FixLay = ThisDrawing.Layers.Add("Fix_Attributes")
    FixLay.Color = acRed

    For Each AT In SelSet
    AT.Color = acByLayer
    AT.Layer = "Fix_Attributes"
    Next
    End If
    ThisDrawing.SelectionSets.Item(0).Delete
    Set SelSet = Nothing
    End Sub

    Private Sub AcadDocument_Activate()
    Call AcadStartUp
    End Sub
     
    Tim Riley, Aug 20, 2003
    #1
  2. Tim Riley

    Ed Jobe Guest

    First, you did not say what the error was. If you are not sure, you might
    improve your error handling. I would break out the part that gets a
    selection set into its own function that returns a SelectionSet object. 1.
    you are using Resume Next and that will interfere with your error handling
    in the main function. 2. You can use it elsewhere over and over again. Then
    use On Error GoTo ErrHandler (whatever you want to name your handler). For
    an example, add a button to a form and see how the wizard creates an error
    handler. In the handler you could even put a message box to tell you what
    error is occurring. Like
    MsgBox err.Number & ": " & err.Description.

    If the code shown is the whole picture, it is not working as you might
    think. ACADStartup, if it is in acad.dvb, will run automatically on startup.
    You do not need to call it. Calling it from the doc.Activate event isn't
    going to work in this case, because apparently event handling is not
    properly set up. You need to do it from a class module.
     
    Ed Jobe, Aug 20, 2003
    #2
  3. Tim Riley

    Kevin Terry Guest

    This is an assumption you are making here, that may not always be true:
    try this instead:
    Public Sub AcadStartUp()
    On Error Goto Exit_Error
    '... your code here...with the exception of the two lines above
    Exit_Error:
    SelSet.Delete
    Set SelSet = Nothing

    HTH,
    Kevin
     
    Kevin Terry, Aug 21, 2003
    #3
  4. Tim Riley

    Tim Riley Guest

    Thanks for the help guys. I modified my code to add what Kevin had
    suggested and I'll see what happens. And Ed, if I omit the call to
    AcadStartup on document activate it does not work. I comment out that line,
    open a drawing that contains 256 attribute definitions, and it does nothing.
    After I uncomment out the line it works.


    Thanks,
    Tim Riley
     
    Tim Riley, Aug 21, 2003
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.