Create Desktop Shortcut

J

Jose Perdigao

The following procedures will create a shortcut in the desktop and it works
fine.
The problem is, the icon shortcut is ms access and I would like create the
shortcut but with my own icon.

Any ideas?

Thanks,
josé perdigão

Public Sub CreateDesktopShortcut(strShortcutTitle As String, Optional
strTargetPath As String = "")
On Error Resume Next

Dim oShell As IWshShell_Class
Dim oShortcut As IWshShortcut_Class
Dim vItem As Variant
Dim vType As Variant

Set oShell = New IWshShell_Class

If strTargetPath = "" Then
strTargetPath = CurrentDb.Name
End If

For Each vItem In oShell.SpecialFolders
If Mid(vItem, Len(vItem) - 6, 7) = "Desktop" And _
InStr(1, vItem, "All Users") = 0 And _
InStr(1, UCase(vItem), "ADMINISTRATOR") = 0 Then
Set oShortcut = oShell.CreateShortcut(vItem & "\" &
strShortcutTitle & ".lnk")
oShortcut.TargetPath = strTargetPath
oShortcut.Save
End If
Next

End Sub

Private Sub cmdCreate_Click()
'For this code to execute you must add a reference to the
'Windows Script Host Object Model (WSHOM.OCX).
Call CreateDesktopShortcut("This is my shortcut title")
End Sub
 
J

John Nurick

Hi José,

IWshShortcut has an IconLocation property. Just set

oShortcut.IconLocation

to the path to your icon. If the icon's in a file that contains multiple
icons, append the index to the path, e.g.(I think)

oShortcut.IconLocation = "D:\Folder\File.dll, 0"
 
J

Jose Perdigao

Thanks a lot, it works fine.

How can I see the properties of IWshShortcut_Class?

form this procedure, I see:
..IconLocation
..TargetPath
..Save

Is it there more?

Thanks
José Perdigão
 
J

John Nurick

WshShell, WshShortcut and the rest are documented in the Windows
Scripting help file, which on this computer is at
"C:\Program Files\Microsoft Windows Script\ScriptDocs\Script56.CHM"

If you don't have it, you can find the documentation by searching
msdn.microsoft.com (where you can also download Windows Scripting stuff
which includes the help file).
 
J

Jose Perdigao

Hi,
The procedure to crate the shortcut in desktop works fine if windoes XP
language version is in english.
I tried in portuguese version doesn't work

Any ideas to change the procedure to work in any language or at least in
englihs or portuguese version?

Thanks,
José Perdigão
 
J

John Nurick

The code you included in your first post looks for the strings "Desktop"
and "All Users" in , and I wouldn't be surprised if they had different
names in Portuguese.

Probably the best thing to do is to get rid of the loop

Dim vItem As Variant

For Each vItem ...
...
Next
and instead just ask for the folder you want, e.g.

Dim strFolder As String

strFolder = oShell.SpecialFolders("AllUsersDesktop")
Set oShortcut = oShell.CreateShortcut(strFolder & "\" & ...

I believe that this
oShell.SpecialFolders("AllUsersDesktop")
will work in any language (without having to translate
"AllUsersDesktop"), but I can't check it here. If you want to put the
shortcut in a different place, the other SpecialFolders constants are
documented under "SpecialFolders Property" in the Scripting help file I
referred you to earlier.
 

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