all userforms close and program right close

  • Thread starter Thread starter Hartmut Callies
  • Start date Start date
H

Hartmut Callies

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
 
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
 
Hartmut said:
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

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. said:
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

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
 
Back
Top