all userforms close and program right close

Discussion in 'AutoCAD' started by Hartmut Callies, Jul 12, 2004.

  1. Hallo,
    I have two simple questions.

    1. How can I close all userforms, when the program close?
    Sub Unload()
    Dim i as Integer

    For i = UserForms.Count To 0 Step -1
    Unload UserForms(i) <<<<<<< Error !
    Next i
    End Sub

    2. How I close a program right? With the command End I get a Error
    in the commandline from AutoCAD (Makroname: Fileload Error ...)!

    Thanks.

    Hartmut Callies
     
    Hartmut Callies, Jul 12, 2004
    #1
  2. Hartmut Callies

    MP Guest

    For i = UserForms.Count To 1 Step -1 ???
    would that work?
    i think collections are 1 based, rather than 0 - but not sure if this helps
    your situation
    is UserForms a built in collection that automatically collects all forms
    created?
    or is it a class you've created to manage the forms for your prog?
    Mark
     
    MP, Jul 12, 2004
    #2
  3. It's understandable that this fails since the number of forms decreases
    with each closed form while the userforms.count is probably just
    interpreted once. So at one point your 'i' is bigger than the remaining
    number of userforms. The error I'd expect is 'index out of range'. I
    would replace this with something like:

    While UserForms.Count>0
    Unload UserForms(0)
    Wend

    Rinze van Huizen
     
    C-Services Holland b.v., Jul 14, 2004
    #3
  4. EErr I didn't see you're counting down :p Anyway, this line
    For i = UserForms.Count To 0 Step -1
    is the culprit, since the array is zero based you need to subtract 1
    from the count i.e.
    For i = UserForms.Count-1 To 0 Step -1

    Since the array is for example
    {0, 1, 2}
    The count is 3, however, the max index is 2. So when you try to close
    form with index 3 it fails.

    Rinze van Huizen
     
    C-Services Holland b.v., Jul 15, 2004
    #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.