word vba to set folder icon

M

MikeB77

Hello
I'm having trouble reliably setting the icon of a new folder that I generate
with filesystemobject. Once I've made a new folder (named 'fldName'), I want
to set its icon. I can do this by adding a desktop.ini file within the
folder:

Dim fs
Set fs = CreateObject("scripting.filesystemobject")
Set getMyRecentPathsFld = fs.createfolder(fldName) 'returns the folder from
my function
FName = fldName & "\Desktop.ini"
System.PrivateProfileString(FName, ".ShellClassInfo", "IconFile") =
"%SystemRoot%\system32\SHELL32.dll"
System.PrivateProfileString(FName, ".ShellClassInfo", "IconIndex") = 155

This generates a desktop.ini as desired. BUT the folder's icon doesn't
change with this code until I first change it manually (via properties...),
after which it works fine. If the folder icon has not been changed manually
the new desktop.ini file looks fine but the icon does not change. I don't
understand why.

Am I missing something here, like some way of telling the folder to look for
a desktop.ini? Or is there a better way?

Thanks in advance
Mike
 
S

Steve Yandl

Mike,

It's been some time since I've set up a routine to change folder icons but
in the somewhat distant past I have used the "Scripting.FileSystemObject" to
create a new text file named desktop.ini, used FSO to insert the correct
lines and then used cmd.exe to change the attributes of desktop.ini to
hidden and system. I still recall having to select the new folder and press
F5 to refresh Explorer to get the change to take effect. It might make your
users angry but you could probably kill the Explorer.exe process and restart
it to have the change of folder icon take effect.

My choice these days is to create a new folder where I stash all my created
new folders and place a shortcut to that folder on the user's desktop.
Icons for shortcuts are a lot simpler to edit than icons for the folder
object.

Steve
 
M

MikeB77

Steve
File attributes - the missing step! I looked at the attributes of folders
(with getAttr) I had set manually then added the following to the code above.
It now changes the icon of the newly created folder:

SetAttr fldName, vbReadOnly ' thats my new folder
SetAttr FName, vbHidden + vbSystem + vbArchive ' thats my 'desktop.ini'
created by System.PrivateProfileString

Thanks Steve for another great tip and more hours of sleep saved.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top