Rubberduck: Parameter 'Cancel' is never used

Created on 3 Feb 2017  路  8Comments  路  Source: rubberduck-vba/Rubberduck

Common in event procedures

Private Sub XLApp_WorkbookBeforeClose(ByVal wb As Workbook, Cancel As Boolean)

bug parse-tree-processing

Most helpful comment

It would be handy too if github showed the last post date/time and author in the general list of issues so I know if I've already replied to that.

All 8 comments

Also Terminate event handler

Procedure 'Terminate' is not used
Not explicitly, but needed to handle object destruction.

I can't replicate to top inspection hit with the current build. Can you supply a minimal example that demonstrates this? Also, are you using the current release build (2.0.11) or did you compile from the current source? There have been several code changes in the identification of event handlers that are relevant.

Tested against the current source with this for the first situation:

'In AppHolder.cls
Option Explicit

Private WithEvents xlApp As Application

Private Sub Class_Initialize()
    Set xlApp = Application
End Sub

Private Sub xlApp_SheetActivate(ByVal Sh As Object)
    Dim win As Window
    Set win = Sh.Parent.Windows(1)
    win.Zoom = 80
End Sub

The second, class Initialize and Terminate are only generating results if the class name is explicitly specified for the event handler:

'No inspection results.
Private Sub Class_Initialize()
    Debug.Print "Foo"
End Sub

'No inspection results.
Private Sub Class_Terminate()
    Debug.Print "Bar"
End Sub

...as opposed to...

'Sub not used.
Private Sub Class1_Initialize()
    Debug.Print "Foo"
End Sub

'Sub not used.
Private Sub Class1_Terminate()
    Debug.Print "Bar"
End Sub

I'm using 2.0.11.2453 downloaded yesterday.

(1)
I took this code from http://www.jkp-ads.com/Articles/keepuserformontop02.asp
Rubberduck also reported:
Dim mhwndForm As LongPtr
Module-Level variable 'mhwndForm' is declared with the 'Dim' keyword.

mhwndForm = FindWindowA("ThunderDFrame", Caption)
Local variable 'Caption' is not declared

Here are the declarations in the Userform code:

Private WithEvents XLApp As Excel.Application

Private Sub XLApp_WindowActivate(ByVal wb As Workbook, ByVal Wn As Window)

Private Sub XLApp_WindowResize(ByVal wb As Workbook, ByVal Wn As Window)

Private Sub XLApp_WorkbookBeforeClose(ByVal wb As Workbook, Cancel As Boolean)

Rubberduck is paying off already!
(2) I found I had a general Sub Terminate() where I should have had a Class sub
Private Sub Class_Terminate()

It would be handy too if github showed the last post date/time and author in the general list of issues so I know if I've already replied to that.

The module-level 'Dim' keyword inspection result is by design: that inspection aims to disambiguate Dim vs Private keywords by encouraging to keep Dim for declaring locals, and Private for fields.

Is Caption an enum value? If so then I believe it's already fixed.

Happy to see Rubberduck help! Can't wait to get 2.0.12 out now ;-)

Re Dim mhwndForm As LongPtr, this is related to #2538. We're currently treating it as a Class instead of treading it as an alias for DWORD like VBE7.dll has it defined. I'll add the bug tag to that issue.

@retailcoder I pulled the linked code - it's intended to be in a UserForm, so Caption is referring to Me.Caption. I replicated the variable not assigned inspection hit for that. If I change that line to call it explicitly and re-running the inspections, it doesn't hit:

mhwndForm = FindWindowA("ThunderDFrame", Me.Caption)

That points to a resolver issue.

I also replicate the implicit ByRef parameter for Cancel. Code to replicate:

'In UserForm1
Private WithEvents XLApp As Excel.Application

Private Sub XLApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
    Debug.Print "Foo"
End Sub

@comintern isn't this one fixed now?

fixed in d2ba220c6e5b62e1c07a9098aa347507acf5aed1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

connerk picture connerk  路  3Comments

Gener4tor picture Gener4tor  路  3Comments

philippetev picture philippetev  路  3Comments

retailcoder picture retailcoder  路  3Comments

eteichm picture eteichm  路  4Comments