Link to home
Start Free TrialLog in
Avatar of Terrygordon
Terrygordon

asked on

Add an icon to an Excel userform titlebar

Hi All

Is it possible to add an icon to an Excel userform titlebar? I have seen a few posts on various sites that purport to do this, for example:

Sub AddIcon()
'Add an icon on the titlebar
Dim hwnd As Long
Dim lngRet As Long
Dim hIcon As Long
hIcon = Sheet2.Image1.Picture.Handle
hwnd = FindWindow(vbNullString, frmMenu.Caption)
lngRet = SendMessage(hwnd, WM_SETICON, ICON_SMALL, ByVal hIcon)
lngRet = SendMessage(hwnd, WM_SETICON, ICON_BIG, ByVal hIcon)
lngRet = DrawMenuBar(hwnd)
End Sub

However, when I run this sub it displays the userform but with no icon. I am running the code from a module in VB just using the 'run' button. The form is Userform2 and the picture I want to use as the icon is contained in image1 on sheet 2 of the workbook. I am using Excel 2007.

Any help would be greatly appreciated.

Regards

Terry
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Can you attach your workbook?
Avatar of Terrygordon
Terrygordon

ASKER

Hi Martin

I don't actually have the workbook. I just created a simple test workbook with a blank userform and an image on sheet2 to see if the code would work before using it on the main project. I've now tried recreating it and I'm getting a "method or data member not found" error on Image1.
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Rory

This seems to work fine with the icon you have placed in the image, but not for jpg or gif formats. Can I assume therefore that the picture has to be a specific type of file and, if so, which type? I have tried using an icon file created with an icon creator software called icoFX, which is recognised as an icon file by the image import dialog, but when I click to open it I get an "Invalid picture" message.

Regards

Terry
It needs to be a valid icon file I believe (.ico).
Thanks Rory. I will have a play with some different formats. The code itself works great.

Regards

Terry
Just for reference if anyone else is using this code, the icon file has to be 24 bits or less. 48 x 48 true color seems to work best in terms of resolution and most icon editors will let you choose this resolution. Anything above 24 will not be imported into the image box.

Regards

Terry