Change Folder Icon

redrock45

New Member
Joined
Feb 5, 2003
Messages
2
Hello,

I was wondering if someone could help me in regards to changing the windows folder icon using VBA behind Excel. I know that you can manually change the icon by clicking on the folder properties, customize, and then change icon. I was hoping to create a program that when a user types "CLOSED" in one of the cells in Excel the program automatically will change the icon of the folder or the color if possible. Thanks in advance!
 

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.
I am looking for basically the same program code but it's like looking for a needle in a haystack. Did you ever find out how to change the folder icon?

Hello,

I was wondering if someone could help me in regards to changing the windows folder icon using VBA behind Excel. I know that you can manually change the icon by clicking on the folder properties, customize, and then change icon. I was hoping to create a program that when a user types "CLOSED" in one of the cells in Excel the program automatically will change the icon of the folder or the color if possible. Thanks in advance!
 
Upvote 0
Try this code, which is a partial solution. Partial because you have to manually refresh the Windows Explorer window to make the new icon appear in parts of the window, i.e. in the Address Bar (if shown) and on the left side of Folders view if the folder isn't the active folder. It changes automatically on the right side of the Folders View, or if the Folders View isn't shown. You'll have to play around and experiment to see what happens on your computer. This has been tested on Windows XP.
Code:
Public Sub Change_Folder_Icon()

    Dim FSO As Object
    Dim folderPath As String, DesktopIni As String
    
    folderPath = "C:\path\to\folder"
    
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
    DesktopIni = folderPath & "Desktop.ini"
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    With FSO
        .GetFolder(folderPath).Attributes = System
        
        If .FileExists(DesktopIni) Then .DeleteFile DesktopIni
        
        With .CreateTextFile(DesktopIni, True)
            .WriteLine "[.ShellClassInfo]"
            .WriteLine "IconFile=%SystemRoot%\system32\SHELL32.dll"
            .WriteLine "IconIndex=12"
            .Close
        End With
        
        .GetFile(DesktopIni).Attributes = Hidden
    End With
   
End Sub

Public Sub Reset_Folder_Icon()

    Dim FSO As Object
    Dim folderPath As String, DesktopIni As String
    
    folderPath = "C:\path\to\folder"
    
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
    DesktopIni = folderPath & "Desktop.ini"

    Set FSO = CreateObject("Scripting.FileSystemObject")
    With FSO
        .GetFolder(folderPath).Attributes = Normal
        If .FileExists(DesktopIni) Then .DeleteFile DesktopIni
    End With
   
End Sub
The code automates the manual steps of changing the icon, as described in the OP. To determine the values for IconFile and IconIndex used in the code, do the manual steps and then open the folder's hidden Desktop.ini in Notepad.
 
Upvote 0
I am looking for basically the same program code but it's like looking for a needle in a haystack. Did you ever find out how to change the folder icon?

I appreciate your reply. Since I first asked for help, I did a few more hours of research and finally found that you can accomplish this task by creating a desktop.ini file within the folder. I have created the "ini" file, but haven't written the vba code yet to append the ini file (once it is placed into the folder) to replace the icon file address, but it seems that I can do this by using a "shell" command and "notepad", once the file is in the directory.

I am working on a cd that automatically opens an "xls" file that creates a folder onto the desktop or other location of the user's choosing, moves files to that folder, and creates shortcuts onto the desktop for five of the files. Everything is completed now except for allowing the user to choose the folder icon from 4 icons (located in the folder). The main intent here is to have nothing manual needing to be done by the user except for choosing the name of the folder. I want everything done automatically, and in the background. I

f the code you sent requires the user to have to refresh windows in some way, it's really not what I'm looking for... although I greatly appreciate your response. Do you have any other suggestions or ideas for me? and am I heading in the right direction?

Scott
 
Upvote 0
I just looked back over your code. I'm an idiot! I read about having to refresh and didn't follow your code through thoroughly. I see that you have already shown me how to append or rewrite the desktop.ini file. Thankyou!!! This might be exactly what I am looking for. I already know that the folder must be attribed as a system file. I still have to allow the user to choose the icon from four pictures. I figure I can change the icons into jpg or something that can be inserted into my program to be seen, and then be chosen accordingly.

Sorry for my previous reply. I jumped before I looked.

Scott
 
Upvote 0
I stated in my last reply that the folder had to be attributed as a system file. That was incorrect. I had to use the cmd.exe to make it read only. If you did so by properties, it didn't work.

Scott
 
Upvote 0
Here is the code I have so far for making the folder read only per cmd.exe

dim ini_file as string
ini_file = "attrib +r " & folderPath
Call Shell("C:\WINDOWS\system32\cmd.exe /c " & ini_file, 1)

The code you sent me works great, but until I manually attrib +r by cmd.exe, my code doesn't work yet.

S
 
Upvote 0
My code works. I backtracked through the code and found that I had forgotten a "\" in the address. What you sent me was great except that the mark as read-only code...

.GetFile(DesktopIni).Attributes = ReadOnly

doesn't work. A hard shell command must be used:

Dim ini_file As String
ini_file = "attrib +r " & folderpath
Call Shell("C:\WINDOWS\system32\cmd.exe /c " & ini_file, 1)


Thank you for all your help.

S
 
Upvote 0
.GetFile(DesktopIni).Attributes = ReadOnly

doesn't work.
That should work if you add the following FSO constants for file attributes which I omitted in the late binding code I posted:
Code:
Const ReadOnly = 1
Const Hidden = 2
Const System = 4
A hard shell command must be used:

Dim ini_file As String
ini_file = "attrib +r " & folderpath
Call Shell("C:\WINDOWS\system32\cmd.exe /c " & ini_file, 1)
But you're not comparing the same thing. That's setting the attributes of the folder, not the desktop.ini file in the folder.

To set the read-only attribute of the folder try one of these instead of the Shell function:
Code:
        .GetFolder(folderPath).Attributes = ReadOnly
        SetAttr folderPath, vbReadOnly
 
Upvote 0
John, perhaps you can help me out here..

I'm trying to accomplish pretty much what you have been talking about here; I want to change the folder icon based on some criteria in an Excel sheet (so others can see which folders have sheets in them that requires some kind of action from them). I tried you code and modified the text for the .ini file, and that seems to work great. However, as you mention, the icon won't actually update/refresh. If I right-click and go to properties the correct icon is displayed there, and by simply clicking ok it changes into the one I want. I don't quite follow what you are discussing in the last couple of posts so I thought I'd rather write here to see if you can help me out. To me setting the folder attributes sounds fine, if that can be accomplished without the ini that's totally fine.

Thanks in advance,
Tobias
 
Upvote 0

Forum statistics

Threads
1,215,906
Messages
6,127,659
Members
449,395
Latest member
Perdi

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
Back
Top