PDA

View Full Version : Solved: how to check if a desktop shortcut exists



drawworkhome
02-06-2010, 02:56 PM
hi,
i am trying to figure out how to test if a desktop short cut exits.
Sub CreateDesktopShortcut()
' =================================================================
' Create a custom icon shortcut on the users desktop
' =================================================================

' Msgbox string variables
Dim szMsg As String
Dim szStyle As String
Dim szTitle As String
Dim a As String


' Change here for the icon's name
Const szIconName As String = "\Folder-A3-1.ico"


' Constant string values, you can replace "Desktop"
' with any Special Folders name to create the shortcut there
Const szlocation As String = "Desktop"
Const szLinkExt As String = ".lnk"


' Object variables
Dim oWsh As Object
Dim oShortcut As Object
' Dim oargs As Object


' String variables
Dim szSep As String
Dim szBookName As String
Dim szBookFullName As String
Dim szPath As String
Dim szDesktopPath As String
Dim szShortcut As String

Stop
' Initialize variables
szSep = Application.PathSeparator
szBookName = szSep & ActiveWorkbook.name
szBookFullName = ActiveWorkbook.FullName
szPath = ActiveWorkbook.path
a = "C:\Documents and Settings\ERIK\My Documents\job files"
' szSep = Application.PathSeparator
' szBookName = szSep & ThisWorkbook.name
' szBookFullName = ThisWorkbook.FullName
' szPath = ThisWorkbook.path


On Error GoTo ErrHandle
' The WScript.Shell object provides functions to read system
' information and environment variables, work with the registry
' and manage shortcuts
Set oWsh = CreateObject("WScript.Shell")

szDesktopPath = oWsh.SpecialFolders(szlocation)

Set objFileSys = CreateObject("Scripting.FileSystemObject")
If objFileSys.fileexists(szlocation) Then
MsgBox "shortcut exists"
Else
MsgBox "shortcut does not exist"
End If

' Get the path where the shortcut will be located
szShortcut = szDesktopPath & szBookName & szLinkExt
' If Not oWsh.(szShortcut) Then Exit Sub

' Make it happen
Set oShortcut = oWsh.CreateShortcut(szShortcut)


' Link it to this file
With oShortcut
.TargetPath = szBookFullName
.IconLocation = a & szIconName
' .IconLocation = szPath & szIconName
.Save
End With


' Explicitly clear memory


' Let the user know it was created ok
szMsg = "Shortcut was created successfully"
szStyle = 0
szTitle = "Success!"
MsgBox szMsg, szStyle, szTitle


Set oWsh = Nothing
Set oShortcut = Nothing
Set oargs = Nothing
Exit Sub


' or if it wasn't
ErrHandle:
szMsg = "Shortcut could not be created"
szStyle = 48
szTitle = "Error!"

MsgBox szMsg, szStyle, szTitle
End Sub

thank you.

drawworkhome
02-06-2010, 03:31 PM
this is what i came up with and seems to work.
'//check if shortcut exists
Set objFileSys = CreateObject("Scripting.FileSystemObject")
If objFileSys.fileexists(szShortcut) Then
MsgBox "shortcut exists"
Else
MsgBox "shortcut does not exist"
' Make it happen
Set oShortcut = oWsh.CreateShortcut(szShortcut)

' Link it to this file
With oShortcut
.TargetPath = szBookFullName
.IconLocation = a & szIconName
' .IconLocation = szPath & szIconName
.Save
End With

End If