VBA Code to Pin/Unpin Windows 10 Taskbar Icon

netspeedz

New Member
Joined
Aug 11, 2011
Messages
12
I have the following VBA code in the Private Sub Workbook_Open() of ThisWorkbook of an Excel Pro 2019 spreadsheet running on Windows 10 Pro 64bit:

VBA Code:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal ClassName As String, ByVal WindowName As String) As Long
Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal Instance As Long, ByVal ExeFileName As String, ByVal IconIndex As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
Const WM_SETICON = &H80
Public Sub SetExcelIcon(ByVal IconPath As String)
  Dim A As Long
  Dim hWnd As Long
  Dim hIcon As Long

  hWnd = FindWindow("XLMAIN", Application.Caption)
  hIcon = ExtractIcon(0, IconPath, 0)

  If hIcon > 1 Then
    Call SendMessage(hWnd, WM_SETICON, True, hIcon)
    Call SendMessage(hWnd, WM_SETICON, False, hIcon)
  End If
End Sub

Public Sub setTaskbarIcon()
  Call SetExcelIcon(ThisWorkbook.Path + "\myicon.ico")
End Sub

I initially thought this code wasn't working until, I remembered another post (which I can not remember who/where) that by right clicking on the taskbar icon and selecting 'Pin to taskbar' and then 'Unpin from taskbar', the icon will display correctly.

My question:
Is there some VBA code that I can add to the Private Sub Workbook_Open() or to Public Sub setTaskbarIcon() to 'Pin to taskbar' and then 'Unpin from Taskbar' to duplicate what I physically did with the mouse? Kind of a 'toggle' of sorts.

Any assistance would be appreciated.
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

netspeedz

New Member
Joined
Aug 11, 2011
Messages
12
In windows7 and later, the approach you are using is shaky at best ... One should use the ITASKLIST3 shell interface instead.

I posted an api class wrapper in the past in this thread (check out post#28) :
Display Userform in TaskBar with custom Icon and Hide Excel (mimicking a standalone application)
Appreciate the quick response.

I see in your code where you are assigning 'image1' to the icon, however, I am not sure how to insert the image and name into the excel spreadsheet I'm using. I was unable to select the images you placed in the 'demo' spreadsheet to get an idea on how to do this.
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,189
Office Version
  1. 2016
Platform
  1. Windows
The ClsXLIconMaker class also allows you to obtain the icon from an ICO file ,if you wish, via the MakeIconFromFile Method, so you don't have to necessarly get the icon from an image embeeded in the worksheet.

So, In your scenario, you would do something along these lines ;
VBA Code:
Sub setTaskbarIcon()

    Set oIcon = New ClsXLIconMaker

    With oIcon
        .MakeIconFromFile IconFile:=ThisWorkBook.Path + "\myicon.ico"
        .ThumbnailTooltip = "myicon"  '<== Optional Tooltip.
        .ApplyIcon
    End With

End Sub
 

netspeedz

New Member
Joined
Aug 11, 2011
Messages
12

ADVERTISEMENT

Sub setTaskbarIcon() Set oIcon = New ClsXLIconMaker With oIcon .MakeIconFromFile IconFile:=ThisWorkBook.Path + "\myicon.ico" .ThumbnailTooltip = "myicon" '<== Optional Tooltip. .ApplyIcon End With End Sub
Again, appreciate the quick response.

I've added the recommended code as a separate module. When opening the Excel spreadsheet, a runtime error that the macro cannot be run (Error 1004).

I also get the same error in my Excel spreadsheet, but your demo copy doesn't report the error.

I've attached a screenshot of the error.

Any assistance on troubleshooting the error would be appreciated.
 

Attachments

  • Screenshot - Excel Error setTaskbarIcon.png
    Screenshot - Excel Error setTaskbarIcon.png
    7.2 KB · Views: 5

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,189
Office Version
  1. 2016
Platform
  1. Windows
There are two seperate codes In post#28.

The first code is the class code, so it must be added to a Class Module (ie:- Not to a Normal Module)
In the VBE, go to Insert> Class Module and add the first code. Then, after you have added the code, you will need to change the default class module name "Class1" to : "ClsXLIconMaker" (without the double quotes) ... This last step is done via the Properties pane located to the left of the code pane.

The second code (ie:- the code that uses the Class ie:- the "setTaskbarIcon" Sub) can be placed in any module ie:- in a standard module or in a workbook module etc...
 

netspeedz

New Member
Joined
Aug 11, 2011
Messages
12
There are two seperate codes In post#28.

The first code is the class code, so it must be added to a Class Module (ie:- Not to a Normal Module)
In the VBE, go to Insert> Class Module and add the first code. Then, after you have added the code, you will need to change the default class module name "Class1" to : "ClsXLIconMaker" (without the double quotes) ... This last step is done via the Properties pane located to the left of the code pane.

The second code (ie:- the code that uses the Class ie:- the "setTaskbarIcon" Sub) can be placed in any module ie:- in a standard module or in a workbook module etc...
Appreciate the clarification.

I did get things to work before receiving your last reply. I removed the modules (normal module and class module) and then exported the two modules from your demo and imported both into the spreadsheet I created and everything worked fine . :)

I decided to go with the setTaskbarIconFromImage() method as I would not be required to distribute an .ico file along with the spreadsheet (although both methods worked well).

Thank you again for your diligence and helpful replies. It is appreciated.

------------
 

Watch MrExcel Video

Forum statistics

Threads
1,128,017
Messages
5,628,153
Members
416,296
Latest member
smartua

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top