remoel
remoel

Reputation: 175

Type mismatch on Adding Icon to Userform

I've been using the same code from this answer and finding the same code whenever I search the internet for this, but I'm always getting a

type mismatch

in FindWindow function in the Sub AddIcon. Also tried this, downloaded the sample and got the same error. Any Idea why? I'm using 64-bit version. Thank you.

Upvotes: 2

Views: 545

Answers (1)

Pᴇʜ
Pᴇʜ

Reputation: 57743

In 64bit version FindWindow returns a LongPtr instead of Long (32 bit).

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
     ByVal lpWindowName As String _
) As LongPtr '<-- FindWindow returns a LongPtr (for 64bit)

Therefore

hWnd = FindWindow(vbNullString, Me.Caption)

fails because hWnd was declared as Long in AddIcon() but should be LongPtr.

You can use

#If VBA7 Then
    Dim hWnd As LongPtr
#Else
    Dim hWnd As Long
#End If

To ensure it works for both 32 and 64 bit versions according to the declarations of the WinAPI functions.

Note: You might check if other variables declared as Long also need to be changed to LongPtr. Therefore just look at the 64bit declarations and what the functions return.

Upvotes: 3

Related Questions