Consulting

Results 1 to 2 of 2

Thread: Solved: how to check if a desktop shortcut exists

  1. #1

    Solved: how to check if a desktop shortcut exists

    hi,
    i am trying to figure out how to test if a desktop short cut exits.
    [VBA]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
    [/VBA]
    thank you.

  2. #2
    this is what i came up with and seems to work.[VBA]
    '//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[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •