Dan Sutton
Dan Sutton

Reputation: 63

Use image in Userform Caption

I am working on a UserForm and I am trying to use an IE/Chrome etc logo in the caption property of the UserForm so that the logo is displayed in the window frame followed by some text.

I have done some browsing and have found the following code online but I get an sub/function not defined error on the line involving ExtractIcon.

UserForm Code

Private Sub UserForm_Initialize 

    Dim strIconPath As String
     Dim lngIcon As Long
     Dim lnghWnd As Long

     ' Change to the path and filename of an icon file
     strIconPath = "C:\Users\suttond\Desktop\Picture2.gif"
     ' Get the icon from the source
     lngIcon = ExtractIcon(0, strIconPath, 0)
     ' Get the window handle of the userform
     lnghWnd = FindWindow("ThunderDFrame", UserForm1.Caption)
     'Set the big (32x32) and small (16x16) icons
     SendMessage lnghWnd, WM_SETICON, True, lngIcon
     SendMessage lnghWnd, WM_SETICON, False, lngIcon

End Sub

Module Code

Private Declare Function FindWindow _
     Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
     ByVal lpWindowName As String) As Long

 Private Declare Function ExtractIcon _
     Lib "shell32.dll" Alias "ExtractIconA" _
    (ByVal hInst As Long, _
     ByVal lpszExeFileName As String, _
     ByVal nIconIndex As Long) As Long

 Private Declare Function SendMessage _
     Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, _
     ByVal wMsg As Long, _
     ByVal wParam As Integer, _
     ByVal lParam As Long) As Long

 Private Const WM_SETICON = &H80

Essentially a small IE Explorer logo would display to the left of text already in the caption.

Edit

Module code functions updated to public to allow them to be called from the initialize code. No longer getting the extract error but the image is not appearing in the UserForm caption.

Upvotes: 0

Views: 1494

Answers (1)

FunThomas
FunThomas

Reputation: 29146

As Mistella and Rory mentioned in the comments of your questions, the functions and the constant needs to be declared as Public. If you declare them as Private, they are only known within the Module, but not in the Form.

Second thing is you need to read the Icon from an ICO-file, not from a gif, so you need to convert it. I use IrfanView for tasks like this, but there are tons of tools (even online) available. I did a quick test and it worked:

enter image description here

Upvotes: 1

Related Questions