cristianbuse / VBA-UserForm-MouseScroll

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

using MouseScroll in Word #3

Closed RainerHH closed 4 years ago

RainerHH commented 4 years ago

I using MouseScroll in Word. It works fine, but:

If MouseScroll is hooked, Error routine not works!

i.e.: if I use your Demo Userform under Word and push the Error button (Zero Divide) Word ends without any message and starts again.

cristianbuse commented 4 years ago

Hi,

It's always a bad idea to use the Global instance of a Form. It doesn't unload properly and causes all sorts of issues. Plus it's a bad programming practice. I should have done it properly.

Could you please replace the method: Sub ShowDemoForm() DemoForm.Show vbModal End Sub

with:

Sub ShowDemoForm() With New DemoForm .Show vbModal End With End Sub

and give it another try. If it fixes the issues then let me know and I'll push the change.

Thanks!

RainerHH commented 4 years ago

No, don’t woks.

Word ends, without any message, without new start.

Kind regards

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Freitag, 12. Juni 2020 21:45 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] using MouseScroll in Word (#3)

Hi,

It's always a bad idea to use the Global instance of a Form. It doesn't unload properly and causes all sorts of issues. Plus it's a bad programming practice. I should have done it properly.

Could you please replace the method: Sub ShowDemoForm() DemoForm.Show vbModal End Sub

with:

Sub ShowDemoForm() With New DemoForm .Show vbModal End With End Sub

and give it another try. If it fixes the issues then let me know and I'll push the change.

Thanks!

— 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/3#issuecomment-643453699 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE65QHJETDVZNP4EI7TRWKAT7ANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE3QTXR7X2DRLE34LTTRWKAT7A5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZNFGAY.gif

RainerHH commented 4 years ago

unhook doesn't clean up properly.

Von: P.Lehmann [mailto:paulhh@gmx.net] Gesendet: Samstag, 13. Juni 2020 04:24 An: 'cristianbuse/VBA-UserForm-MouseScroll' reply@reply.github.com Betreff: AW: [cristianbuse/VBA-UserForm-MouseScroll] using MouseScroll in Word (#3)

No, don’t woks.

Word ends, without any message, without new start.

Kind regards

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Freitag, 12. Juni 2020 21:45 An: cristianbuse/VBA-UserForm-MouseScroll <VBA-UserForm-MouseScroll@noreply.github.com mailto:VBA-UserForm-MouseScroll@noreply.github.com > Cc: RainerHH <paulhh@gmx.net mailto:paulhh@gmx.net >; Author <author@noreply.github.com mailto:author@noreply.github.com > Betreff: Re: [cristianbuse/VBA-UserForm-MouseScroll] using MouseScroll in Word (#3)

Hi,

It's always a bad idea to use the Global instance of a Form. It doesn't unload properly and causes all sorts of issues. Plus it's a bad programming practice. I should have done it properly.

Could you please replace the method: Sub ShowDemoForm() DemoForm.Show vbModal End Sub

with:

Sub ShowDemoForm() With New DemoForm .Show vbModal End With End Sub

and give it another try. If it fixes the issues then let me know and I'll push the change.

Thanks!

— 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/3#issuecomment-643453699 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE65QHJETDVZNP4EI7TRWKAT7ANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE3QTXR7X2DRLE34LTTRWKAT7A5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZNFGAY.gif

cristianbuse commented 4 years ago

I forgot about the Unload bug (i believe it's from one of last year's updates from MS).

Try changing the following code in the form: Private Sub btnClose_Click() Unload Me End Sub

to

Private Sub btnClose_Click() Me.Hide End Sub

And make sure you have the previous change too: Sub ShowDemoForm() DemoForm.Show vbModal End Sub

replaced with:

Sub ShowDemoForm() With New DemoForm .Show vbModal End With End Sub

RainerHH commented 4 years ago

I mentioned the unload error because: I used it in a big project.  I had a bug and Word just ended with no information.

Then I restarted Word, did not call the bug, commented out the hook statements, restarted the program without restarting word.  And, what wonder, the mouse hook was still active. Not canceled, despite the unhook message.

Am 13. Juni 2020 22:05, um 22:05, Cristian Buse notifications@github.com schrieb:

I forgot about the Unload bug (i believe it's from one of last year's updates from MS).

Try changing the following code in the form: Private Sub btnClose_Click() Unload Me End Sub

to

Private Sub btnClose_Click() Me.Hide End Sub

And make sure you have the previous change too: Sub ShowDemoForm() DemoForm.Show vbModal End Sub

replaced with:

Sub ShowDemoForm() With New DemoForm .Show vbModal End With End Sub

-- 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/3#issuecomment-643671419

RainerHH commented 4 years ago

frankly, i don't understand the difference between excel and word.

Am 13. Juni 2020 22:05, um 22:05, Cristian Buse notifications@github.com schrieb:

I forgot about the Unload bug (i believe it's from one of last year's updates from MS).

Try changing the following code in the form: Private Sub btnClose_Click() Unload Me End Sub

to

Private Sub btnClose_Click() Me.Hide End Sub

And make sure you have the previous change too: Sub ShowDemoForm() DemoForm.Show vbModal End Sub

replaced with:

Sub ShowDemoForm() With New DemoForm .Show vbModal End With End Sub

-- 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/3#issuecomment-643671419

cristianbuse commented 4 years ago

I do not use Word that much so I can't really say, but clearly the issue is not happening in Excel.

Did you change the "Unload Me" statement to "Me.Hide"? Do you still experience crashes?

If yes, what version of Word are you using? (I use 16.0.12827.20236) 64-bit 365)

RainerHH commented 4 years ago

Yes I done.

  1. I used Demo as a AutoNew macro in a Template I starts the Template the Userform is shown press Error Button Word crashes without any message

  2. I starts Demo from VBE the Userform is shown press Error Button Word crashes without any message Word starts new

The Event Manger says error in VBE7.DLL.

I ‘m using Word 2013.

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Samstag, 13. Juni 2020 23:53 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] using MouseScroll in Word (#3)

I do not use Word that much so I can't really say, but clearly the issue is not happening in Excel.

Did you change the "Unload Me" statement to "Me.Hide"? Do you still experience crashes?

If yes, what version of Word are you using? (I use 16.0.12827.20236) 64-bit 365)

— 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/3#issuecomment-643682973 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE2OOUFHVKFUHIPVFWTRWPYKTANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE3ZGNTKEVPNXKOXESDRWPYKTA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZO5FHI.gif

RainerHH commented 4 years ago

I tested it in Word 2019, the same occurs.

Von: P.Lehmann [mailto:paulhh@gmx.net] Gesendet: Sonntag, 14. Juni 2020 09:19 An: 'cristianbuse/VBA-UserForm-MouseScroll' reply@reply.github.com Betreff: AW: [cristianbuse/VBA-UserForm-MouseScroll] using MouseScroll in Word (#3)

Yes I done.

  1. I used Demo as a AutoNew macro in a Template I starts the Template the Userform is shown press Error Button Word crashes without any message

  2. I starts Demo from VBE the Userform is shown press Error Button Word crashes without any message Word starts new

The Event Manger says error in VBE7.DLL.

I ‘m using Word 2013.

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Samstag, 13. Juni 2020 23:53 An: cristianbuse/VBA-UserForm-MouseScroll <VBA-UserForm-MouseScroll@noreply.github.com mailto:VBA-UserForm-MouseScroll@noreply.github.com > Cc: RainerHH <paulhh@gmx.net mailto:paulhh@gmx.net >; Author <author@noreply.github.com mailto:author@noreply.github.com > Betreff: Re: [cristianbuse/VBA-UserForm-MouseScroll] using MouseScroll in Word (#3)

I do not use Word that much so I can't really say, but clearly the issue is not happening in Excel.

Did you change the "Unload Me" statement to "Me.Hide"? Do you still experience crashes?

If yes, what version of Word are you using? (I use 16.0.12827.20236) 64-bit 365)

— 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/3#issuecomment-643682973 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE2OOUFHVKFUHIPVFWTRWPYKTANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE3ZGNTKEVPNXKOXESDRWPYKTA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZO5FHI.gif

cristianbuse commented 4 years ago

It looks like the difference between Excel and Word is: Excel: on error, the VBE window activates which is being picked up by the IsVBEActive and mouse is unhooked

Word: on error, the Window (Form) is destroyed entirely and VBE window never activates. Error trapping at a higher level doesn't work at all.

This will need more investigation. I will have a look over the following week.

RainerHH commented 4 years ago

If I can be of any help ...

Am 14. Juni 2020 14:53, um 14:53, Cristian Buse notifications@github.com schrieb:

It looks like the difference between Excel and Word is: Excel: on error, the VBE window activates which is being picked up by the IsVBEActive and mouse is unhooked

Word: on error, the Window (Form) is destroyed entirely and VBE window never activates. Error trapping at a higher level doesn't work at all.

This will need more investigation. I will have a look over the following week.

-- 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/3#issuecomment-643762808

RainerHH commented 4 years ago

You wrote that in Excel VBE is recognized by an error and an unhook is performed. I tested it, I can't see an Unhook Debug message after an error.

Am 14. Juni 2020 15:26, um 15:26, Paul Lehmann paulhh@gmx.net schrieb:

If I can be of any help ...

Am 14. Juni 2020 14:53, um 14:53, Cristian Buse notifications@github.com schrieb:

It looks like the difference between Excel and Word is: Excel: on error, the VBE window activates which is being picked up by the IsVBEActive and mouse is unhooked

Word: on error, the Window (Form) is destroyed entirely and VBE window never activates. Error trapping at a higher level doesn't work at all.

This will need more investigation. I will have a look over the following week.

-- 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/3#issuecomment-643762808

cristianbuse commented 4 years ago

In Excel, once you generate an error the VBE tries to become the Active Window and the "IsVBEActive" function returns True. From within the "MouseProc" method the "Unhook..." is called which displays a message in the Immediate window (e.g. Mouse unhooked 15/06/2020 08:00:36). Once the "MouseProc" methods returns, the error pops-up (Division by Zero). Tested on Win 2013/2016 on both 32 and 64 bits.

If you are not seeing the "Division by Zero" error from the Demo then there must be something specific to your system/configuration.

cristianbuse commented 4 years ago

Try removing these 3 lines (or comment them) from the "HookMouseToForm" method: On Error Resume Next CallByName Application, "EnableCancelKey", VbLet, 0 On Error GoTo 0

It solves the issue on my end.

RainerHH commented 4 years ago

Answer to your penultimate answer:

You will see the Unhook Message because the VBE Error Window “Debug or End” appears,

NOT because an error has occurred.

You can test it by catching the error with On Error Unit. I.e.:

Private Sub CommandButton2_Click()

On Error GoTo eh

Debug.Print 1 / 0

eh: MsgBox "Error"

End Sub

Answer to your last answer:

YES, it works.

But is the call unnecessary?

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Montag, 15. Juni 2020 09:59 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] using MouseScroll in Word (#3)

Try removing these 3 lines (or comment them) from the "HookMouseToForm" method: On Error Resume Next CallByName Application, "EnableCancelKey", VbLet, 0 On Error GoTo 0

It solves the issue on my end.

— 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/3#issuecomment-643968327 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOEZHWXTSLJDGIZUXNITRWXIEFANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE7PBWEBI3BLZPY2ZNLRWXIEFA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZRC2RY.gif

cristianbuse commented 4 years ago

When the VBE Error Window “Debug or End” appears the code MUST unhook. Otherwise MouseProc callback wouls stop to be called and because the operating system has a max timeout for hooks a crash would occur.

The EnableCancelKey call was in there to prevent the Users to Ctrl+Break into the Debugger. The reason is using CallByName is because not all Applications have that property (e.g. PowerPoint). It should work for Word but for some reason is causes some weird behaviour like the one you've experienced. I will check if I can keep it for Word.

RainerHH commented 4 years ago

Yes of course, when Debug or end, then Unhook!

Ok, thanks!

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Montag, 15. Juni 2020 10:49 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] using MouseScroll in Word (#3)

When the VBE Error Window “Debug or End” appears the code MUST unhook. Otherwise MouseProc callback wouls stop to be called and because the operating system has a max timeout for hooks a crash would occur.

The EnableCancelKey call was in there to prevent the Users to Ctrl+Break into the Debugger. The reason is using CallByName is because not all Applications have that property (e.g. PowerPoint). It should work for Word but for some reason is causes some weird behaviour like the one you've experienced. I will check if I can keep it for Word.

— 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/3#issuecomment-643994319 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOEZ666AWBGBTB77YOFDRWXOAVANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE2FEJ3S2K5DN5J2VRDRWXOAVA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZRJFTY.gif

RainerHH commented 4 years ago

If I use

    'CallByName Application, "EnableCancelKey", VbLet, 0

    Application.EnableCancelKey = wdCancelDisabled

then the userform ends without any message, but word don’t crashes.

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Montag, 15. Juni 2020 10:49 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] using MouseScroll in Word (#3)

When the VBE Error Window “Debug or End” appears the code MUST unhook. Otherwise MouseProc callback wouls stop to be called and because the operating system has a max timeout for hooks a crash would occur.

The EnableCancelKey call was in there to prevent the Users to Ctrl+Break into the Debugger. The reason is using CallByName is because not all Applications have that property (e.g. PowerPoint). It should work for Word but for some reason is causes some weird behaviour like the one you've experienced. I will check if I can keep it for Word.

— 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/3#issuecomment-643994319 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOEZ666AWBGBTB77YOFDRWXOAVANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE2FEJ3S2K5DN5J2VRDRWXOAVA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZRJFTY.gif

RainerHH commented 4 years ago

But hangs after restart the userform

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Montag, 15. Juni 2020 10:49 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] using MouseScroll in Word (#3)

When the VBE Error Window “Debug or End” appears the code MUST unhook. Otherwise MouseProc callback wouls stop to be called and because the operating system has a max timeout for hooks a crash would occur.

The EnableCancelKey call was in there to prevent the Users to Ctrl+Break into the Debugger. The reason is using CallByName is because not all Applications have that property (e.g. PowerPoint). It should work for Word but for some reason is causes some weird behaviour like the one you've experienced. I will check if I can keep it for Word.

— 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/3#issuecomment-643994319 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOEZ666AWBGBTB77YOFDRWXOAVANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE2FEJ3S2K5DN5J2VRDRWXOAVA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZRJFTY.gif

RainerHH commented 4 years ago

I don’t understand that The EnableCancelKey call was in there to prevent the Users to Ctrl+Break into the Debugger.

because when i go into the debugger VBE is recognized and Unhook will be done?

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Montag, 15. Juni 2020 10:49 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] using MouseScroll in Word (#3)

When the VBE Error Window “Debug or End” appears the code MUST unhook. Otherwise MouseProc callback wouls stop to be called and because the operating system has a max timeout for hooks a crash would occur.

The EnableCancelKey call was in there to prevent the Users to Ctrl+Break into the Debugger. The reason is using CallByName is because not all Applications have that property (e.g. PowerPoint). It should work for Word but for some reason is causes some weird behaviour like the one you've experienced. I will check if I can keep it for Word.

— 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/3#issuecomment-643994319 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOEZ666AWBGBTB77YOFDRWXOAVANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE2FEJ3S2K5DN5J2VRDRWXOAVA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZRJFTY.gif

cristianbuse commented 4 years ago

Ctrl+Break is like jumping to a break-point. Code stops entirely. Using a Mouse Hook (on x64) will crash the Application if the code is stopped because the callback (MouseProc) cannot be called anymore. Meanwhile, if you generate an untrapped error that brings up the End/Debug dialog, the code still runs and allows an Unhook just-in-time.

I've tried to see if there's a way to keep the EnableCancelKey as disabled but no luck so far.

RainerHH commented 4 years ago

What is Ctrl+Break, i know what Ctrl is (strg), but what means Break ?

I have created a breakpoint. Execution stops. And I can go to the VBE, makes single steps (F8). All works.

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Montag, 15. Juni 2020 12:03 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] using MouseScroll in Word (#3)

Ctrl+Break is like jumping to a break-point. Code stops entirely. Using a Mouse Hook (on x64) will crash the Application if the code is stopped because the callback (MouseProc) cannot be called anymore. Meanwhile, if you generate an untrapped error that brings up the End/Debug dialog, the code still runs and allows an Unhook just-in-time.

I've tried to see if there's a way to keep the EnableCancelKey as disabled but no luck so far.

— 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/3#issuecomment-644032737 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE52KSRPF5GNPELQO73RWXWVFANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE5M7ELXWN5GFTBH55DRWXWVFA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZRSRYI.gif

cristianbuse commented 4 years ago

Sorry. I did not realize it could be confusing. I meant keyboard combination Ctrl+Break wher you keep the Ctrl button pressed and then you press the Break key which is usually above the PageUp key on a normal keyboard. This key combination works in VBA regardless of the host Application (Excel, Word, AutoCAD etc.) hence the need to set the EnableCancelKey to 0/Disabled so that users cannot Ctrl+Break into running code.

RainerHH commented 4 years ago

I tested Ctrl+PgUp (respectively Strg+PgUp), it works as I use PgUp only! Without Stops, without Crashes.

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Montag, 15. Juni 2020 15:05 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] using MouseScroll in Word (#3)

Sorry. I did not realize it could be confusing. I meant keyboard combination Ctrl+Break wher you keep the Ctrl button pressed and then you press the Break key which is usually above the PageUp key on a normal keyboard. This key combination works in VBA regardless of the host Application (Excel, Word, AutoCAD etc.) hence the need to set the EnableCancelKey to 0/Disabled so that users cannot Ctrl+Break into running code.

— 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/3#issuecomment-644122226 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOEYRG4LHCZAJFTLBKSDRWYMABANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE2OT3HGCZPFR7CINULRWYMABA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZSIM4Q.gif

cristianbuse commented 4 years ago

Pause

On some keyboards it's called Pause instead of Break

RainerHH commented 4 years ago

Ok, I had missed "above".

With Ctrl + Break I end up in the VBE.

I can then continue to run.

But when I stop execution, VBE gets into a loop and then crashes Word.

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Montag, 15. Juni 2020 15:39 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] using MouseScroll in Word (#3)

Pause https://defkey.com/content/images/news/2018-09-09_02-34-49-pausebreak-inline.jpg

On some keyboards it's called Pause instead of Break

— 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/3#issuecomment-644140814 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE7VRVDPBRNNW7J6XXTRWYP6NANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE6LNJCDKGUEI6Q5O6DRWYP6NA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZSM6DQ.gif

RainerHH commented 4 years ago

I have found the difference between Excel an Word:

Excel:

The EnableCancelKey property is always reset to xlInterrupt whenever Microsoft Excel returns to the idle state and there's no code running.

Word:

the EnableCancelKey property is not reset to wdCancelInterrupt when your code stops running

Von: Cristian Buse [mailto:notifications@github.com] Gesendet: Montag, 15. Juni 2020 15:39 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] using MouseScroll in Word (#3)

Pause https://defkey.com/content/images/news/2018-09-09_02-34-49-pausebreak-inline.jpg

On some keyboards it's called Pause instead of Break

— 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/3#issuecomment-644140814 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7ZOE7VRVDPBRNNW7J6XXTRWYP6NANCNFSM4N4PUT3A . https://github.com/notifications/beacon/AE7ZOE6LNJCDKGUEI6Q5O6DRWYP6NA5CNFSM4N4PUT3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZSM6DQ.gif

cristianbuse commented 4 years ago

Thanks. I have found that as well. The problem is that even if I set it back to wdCancelInterrupt, at the moment when the Unhook happens, it's not displaying the Debug/End Window and it just closes the form. Need to find another way around this.

cristianbuse commented 4 years ago

After several hours trying to find a way around the EnableCancelKey issue, apparently there is none. See the 2 comments in method below: Sub Test() Application.EnableCancelKey = wdCancelDisabled Debug.Print 1 / 0 'Code exits here Application.EnableCancelKey = wdCancelInterrupt 'Code never reches this line End Sub

If EnableCancelKey is set to wdCancelDisabled then you would never get the Debug/End Error Window unless you: 1) trap the error; 2) restore EnableCancelKey to wdCancelInterrupt and 3) Resume error

Sub Test2() Application.EnableCancelKey = wdCancelDisabled On Error GoTo ErrorHandler Debug.Print 1 / 0 'Jumps to ErrorHandler Exit Sub ErrorHandler: Application.EnableCancelKey = wdCancelInterrupt On Error GoTo 0 'Avoid infinite loop Resume End Sub

Of course, adding error handling to all methods would be overkill as you don't need Error Handling in all methods. The problem is even worse when you set Application.EnableCancelKey = wdCancelDisabled for a Modal Userform because you cannot do the error trapping before the .Show call (in the procedure calling the form).

Note that so far this had nothing to do with the Mouse hook. Just Word functionality.

Unfortunately you are left with 2 bad options when using the mouse scroll library (in Word): 1) Set Application.EnableCancelKey = wdCancelDisabled when hooking the mouse and then set Application.EnableCancelKey = wdCancelInterrupt when unhooking. Pros: User cannot Ctrl+Break into Form and cause a crash Cons: Any unhandled error simply closes the form and sometimes can cause a crash 2) Do not change the Application.EnableCancelKey Pros: Unhandled error would display the End/Debug Error dialog and unhook properly Cons: User can Ctrl+Break into Form and cause a crash

I would choose option 1 and make sure that code is error-proof so that the form doesn't close.

I've pushed the changes according to Option 1. I will close this issue as there is nothing more I can do here.

Again thanks for pointing the issue and for the research.

RainerHH commented 4 years ago

Thanks! I choose option 2.  The users don't know Ctrl + Break at all.

Am 15. Juni 2020 23:19, um 23:19, Cristian Buse notifications@github.com schrieb:

After several hours trying to find a way around the EnableCancelKey issue, apparently there is none. See the 2 comments in method below: Sub Test() Application.EnableCancelKey = wdCancelDisabled Debug.Print 1 / 0 'Code exits here Application.EnableCancelKey = wdCancelInterrupt 'Code never reches this line End Sub

If EnableCancelKey is set to wdCancelDisabled then you would never get the Debug/End Error Window unless you: 1) trap the error; 2) restore EnableCancelKey to wdCancelInterrupt and 3) Resume error

Sub Test2() Application.EnableCancelKey = wdCancelDisabled On Error GoTo ErrorHandler Debug.Print 1 / 0 'Jumps to ErrorHandler Exit Sub ErrorHandler: Application.EnableCancelKey = wdCancelInterrupt On Error GoTo 0 'Avoid infinite loop Resume End Sub

Of course, adding error handling to all methods would be overkill as you don't need Error Handling in all methods. The problem is even worse when you set Application.EnableCancelKey = wdCancelInterrupt for a Modal Userform because you cannot do the error trapping before the .Show call (in the procedure calling the form).

Note that so far this had nothing to do with the Mouse hook. Just Word functionality.

Unfortunately you are left with 2 bad options when using the mouse scroll library (in Word): 1) Set Application.EnableCancelKey = wdCancelDisabled when hooking the mouse and then set Application.EnableCancelKey = wdCancelInterrupt when unhooking. Pros: User cannot Ctrl+Break into Form and cause a crash Cons: Any unhandled error simply closes the form and sometimes can cause a crash 2) Do not change the Application.EnableCancelKey Pros: Unhandled error would display the End/Debug Error dialog and unhook properly Cons: User can Ctrl+Break into Form and cause a crash

I would choose the option 1 and make sure that code is error-proof so that the form doesn't close.

I've pushed the changes according to Option 1. I will close this issue as there is nothing more I can do here.

Again thanks for pointing the issue and for the research.

-- 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/3#issuecomment-644396288

cristianbuse commented 11 months ago

Starting 76a384fb50d5bb2a5f6de49e287fe2fb774472d6 both Modal and Modeless forms are supported while debugging also works and there is no need to use Application.EnableCancelKey anymore