cristianbuse / VBA-UserForm-MouseScroll

Use the Mouse Scroll Wheel to scroll VBA UserForms and Controls
MIT License
71 stars 13 forks source link

no Unhook by UserForm Unload #4

Closed RainerHH closed 4 years ago

RainerHH commented 4 years ago

"Unload Me" does not unhook.

Causes Word to be restarted sometimes (probably due to a previous error).

Tested in Word

cristianbuse commented 4 years ago

I've stopped using the Unload method since late 2018. There is a bug that causes crashes. NOT related with the Mouse Scroll library. I have experienced crashes too many times using the Unload so I am now using Me.Hide. If you are using the global Form instance stop that too. Start using New instances of the Forms and use .Hide. They will unload themselfs when the form instance goes out of scope (or sometimes later) but with no crash.

If you look at the library, I've actually changed the Unload Me a few days ago when we were discussing the other bug with the .EnableCancelKey

RainerHH commented 4 years ago

Can i use me.hide and then Unload ?

Am 17. Juni 2020, 10:08, um 10:08, Cristian Buse notifications@github.com schrieb:

I've stopped using the Unload method since late 2018. There is a bug that causes crashes. NOT related with the Mouse Scroll library. I have experienced crashes too many times using the Unload so I am now using Me.Hide. If you are using the global Form instance stop that too. Start using New instances of the Forms and use .Hide. They will unload themselfs when the form instance goes out of scope (or sometimes later) but with no crash.

If you look at the library, I've actually changed the Unload Me a few days ago when we were discussing the other bug with the .EnableCancelKey

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/cristianbuse/VBA-UserForm-MouseScroll/issues/4#issuecomment-645222774

cristianbuse commented 4 years ago

Just Me.Hide

Let me explain. Assume the form name is DemoForm.

If you use the Global instance of the form like this: Sub Test .... DemoForm.Show .... End Sub the form will remain loaded in memory if you do not use Unload.

On the other hand, if you use a New instance: Sub Test2 .... With New DemoForm .Show End With .... End Sub you do not need to use Unload because the instance will lose scope once the End With line is called

Similarly, if you use a New instance but assign to a variable: Sub Test3 .... dim frm as New DemoForm With frm .Show End With .... End Sub you still do not need to use Unload because the instance will lose scope once the End Sub line is called.

Sometimes, for both example 2 and 3 above, the new form instance might still remain loaded in memory for a while but eventually it will get Unloaded without you having to do anything.

If you want to test, then add a Teminate event in the form and voila. Private Sub UserForm_Terminate() Debug.Print "Terminated " & Now End Sub You will see exactly when things get Unloaded

RainerHH commented 4 years ago

I must do Unload, because i need the Initial sub again by the next show.

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Mittwoch, 17. Juni 2020 10:36 An: cristianbuse/VBA-UserForm-MouseScroll VBA-UserForm-MouseScroll@noreply.github.com Cc: RainerHH paulhh@gmx.net; Author author@noreply.github.com Betreff: Re: [cristianbuse/VBA-UserForm-MouseScroll] no Unhook by UserForm Unload (#4)

Just Me.Hide

Let me explain. Assume the form name is DemoForm.

If you use the Global instance of the form like this: Sub Test .... DemoForm.Show .... End Sub the form will remain loaded in memory if you do not use Unload.

On the other hand, if you use a New instance: Sub Test2 .... With New DemoForm .Show End With .... End Sub you do not need to use Unload because the instance will lose scope once the End With line is called

Similarly, if you use a New instance but assign to a variable: Sub Test3 .... dim frm as New DemoForm With frm .Show End With .... End Sub you still do not need to use Unload because the instance will lose scope once the End Sub line is called.

Sometimes, for both example 2 and 3 above, the new form instance might still remain loaded in memory for a while but eventually it will get Unloaded without you having to do anything.

If you want to test, then add a Teminate event in the form and voila. Private Sub UserForm_Terminate() Debug.Print "Terminated " & Now End Sub You will see exactly when things get Unloaded

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cristianbuse/VBA-UserForm-MouseScroll/issues/4#issuecomment-645237030 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE62E3RXEK7HIUODG5LRXB6AZANCNFSM4OAJP3TA . https://github.com/notifications/beacon/AE7ZOE2GCQNQNVH2DS222A3RXB6AZA5CNFSM4OAJP3TKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZ2YSJQ.gif

cristianbuse commented 4 years ago

I am sorry. I do not understand what you mean by "Initial Sub"

RainerHH commented 4 years ago

I mena „Private Sub UserForm_Initialize()”

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Mittwoch, 17. Juni 2020 12:48 An: cristianbuse/VBA-UserForm-MouseScroll VBA-UserForm-MouseScroll@noreply.github.com Cc: RainerHH paulhh@gmx.net; Author author@noreply.github.com Betreff: Re: [cristianbuse/VBA-UserForm-MouseScroll] no Unhook by UserForm Unload (#4)

I am sorry. I do not understand what you mean by "Initial Sub"

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cristianbuse/VBA-UserForm-MouseScroll/issues/4#issuecomment-645300283 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOEZSI5IZIB5J4MSAGX3RXCNOXANCNFSM4OAJP3TA . https://github.com/notifications/beacon/AE7ZOE7RZROD53CONUSWA43RXCNOXA5CNFSM4OAJP3TKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZ3IAOY.gif

cristianbuse commented 4 years ago

It will run for each instance. Try it and you'll see.

RainerHH commented 4 years ago

But I can also use

Me.Hide

Unload Me

?

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Mittwoch, 17. Juni 2020 13:07 An: cristianbuse/VBA-UserForm-MouseScroll VBA-UserForm-MouseScroll@noreply.github.com Cc: RainerHH paulhh@gmx.net; Author author@noreply.github.com Betreff: Re: [cristianbuse/VBA-UserForm-MouseScroll] no Unhook by UserForm Unload (#4)

It will run for each instance. Try it and you'll see.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cristianbuse/VBA-UserForm-MouseScroll/issues/4#issuecomment-645308260 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE2743VOSBWOFC4WL2DRXCPVVANCNFSM4OAJP3TA . https://github.com/notifications/beacon/AE7ZOE6CHDHPYEC2L4IHROLRXCPVVA5CNFSM4OAJP3TKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZ3J6ZA.gif

cristianbuse commented 4 years ago

You could use the Unload outside the Form to be safe but again you would be subject to the same bug. You do not need to use Unload.

Try this exercise. Create a Form called frmTest. Add a command button (should be called CommandButton1 by default. Add this code to form:

Option Explicit
Private Sub CommandButton1_Click()
    Me.Hide
End Sub
Private Sub UserForm_Initialize()
    Debug.Print "Form was initialized at " & Now
End Sub
Private Sub UserForm_Terminate()
    Debug.Print "Form was terminated at " & Now
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Cancel = True
        Me.Hide
    End If
End Sub

Now add this method to a standard module (Module1 for example) and Run it:

Sub TestForm()
    Dim i As Long

    For i = 1 To 3
        With New frmTest
            .Show
        End With
    Next i
End Sub

3 instances of the form (the form is a Class) are created and destroyed

RainerHH commented 4 years ago

I don’t understand

When I use Me.Hide and this makes a Unhook, why causes the then Unload Me the same error?

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Mittwoch, 17. Juni 2020 13:19 An: cristianbuse/VBA-UserForm-MouseScroll VBA-UserForm-MouseScroll@noreply.github.com Cc: RainerHH paulhh@gmx.net; Author author@noreply.github.com Betreff: Re: [cristianbuse/VBA-UserForm-MouseScroll] no Unhook by UserForm Unload (#4)

You could use the Unload outside the Form to be safe but again you would be subject to the same bug. You do not need to use Unload.

Try this exercise. Create a Form called frmTest. Add a command button (should be called CommandButton1 by default. Add this code to form: Option Explicit Private Sub CommandButton1_Click() Me.Hide End Sub Private Sub UserForm_Initialize() Debug.Print "Form was initialized at " & Now End Sub Private Sub UserForm_Terminate() Debug.Print "Form was terminated at " & Now End Sub

Now add this method to a standard module (Module1 for example) and Run it: Sub TestForm() Dim i As Long

For i = 1 To 3 With New frmTest .Show End With Next i

End Sub

3 instances of the form (the form is a Class) are created and destroyed

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cristianbuse/VBA-UserForm-MouseScroll/issues/4#issuecomment-645313368 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE662YQCDIPTBUIS5EDRXCRDBANCNFSM4OAJP3TA . https://github.com/notifications/beacon/AE7ZOEZCGLOJT72AGKJK4MDRXCRDBA5CNFSM4OAJP3TKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZ3LGWA.gif

cristianbuse commented 4 years ago

Bcause the Unload Me has a separate bug that is unrelated to this project. That is why i do not use it

RainerHH commented 4 years ago

I'm still going crazy.

In my project, when I work with With, my initialization routine is always called twice.

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Mittwoch, 17. Juni 2020 13:30 An: cristianbuse/VBA-UserForm-MouseScroll VBA-UserForm-MouseScroll@noreply.github.com Cc: RainerHH paulhh@gmx.net; Author author@noreply.github.com Betreff: Re: [cristianbuse/VBA-UserForm-MouseScroll] no Unhook by UserForm Unload (#4)

Bcause the Unload Me has a separate bug that is unrelated to this project

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cristianbuse/VBA-UserForm-MouseScroll/issues/4#issuecomment-645317778 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE6G5IN7C7B25YT222DRXCSKFANCNFSM4OAJP3TA . https://github.com/notifications/beacon/AE7ZOE2FCHUNHGVKT7YGB7TRXCSKFA5CNFSM4OAJP3TKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZ3MJEQ.gif

cristianbuse commented 4 years ago

That should not happen. Feel free to send me your code (a minimal version) at cristian.buse@yahoo.com and I will have a look

cristianbuse commented 4 years ago

I'm closing this issue. Using Me.Hide instead or before Unload seems to solve the issue. Alternatively you can add a UserForm_Terminate event in your Form and Unhook there