CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2001
    Posts
    1

    Create a desktop shortcut

    Hi,

    Does anyone know how to create a desktop shortcut to a file using VBA? I need to be able to control the target string from within VBA aswell.

    Thanks,

    Joe Taylor


  2. #2

    Re: Create a desktop shortcut

    Try with Wscript.Shell object

    Dim WshShell
    Set WshShell = CreateObject("Wscript.Shell")
    Set oShellLink = WshShell.CreateShortcut("c:\mylink.lnk")
    oShellLink.TargetPath = "c:\myfile.txt" 'U must specify the absolute path
    oShellLink.Save

    For create shourtcut on desktop U must use API to get Desktop folder.

    hi, brt


  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Create a desktop shortcut

    Example from MS

    ' This sample demonstrates how to use the WSHShell object to create a shortcut
    ' on the desktop.

    L_Welcome_MsgBox_Message_Text = "This script will create a shortcut to Notepad on your desktop."
    L_Welcome_MsgBox_Title_Text = "Windows Scripting Host Sample"
    Call Welcome()

    ' ********************************************************************************
    ' *
    ' * Shortcut related methods.
    ' *

    Dim WSHShell
    Set WSHShell = WScript.CreateObject("WScript.Shell")


    Dim MyShortcut, MyDesktop, DesktopPath

    ' Read desktop path using WshSpecialFolders object
    DesktopPath = WSHShell.SpecialFolders("Desktop")

    ' Create a shortcut object on the desktop
    Set MyShortcut = WSHShell.CreateShortcut(DesktopPath & "\Shortcut to notepad.lnk")

    ' Set shortcut object properties and save it
    MyShortcut.TargetPath = WSHShell.ExpandEnvironmentStrings("%windir%\notepad.exe")
    MyShortcut.WorkingDirectory = WSHShell.ExpandEnvironmentStrings("%windir%")
    MyShortcut.WindowStyle = 4
    MyShortcut.IconLocation = WSHShell.ExpandEnvironmentStrings("%windir%\notepad.exe, 0")
    MyShortcut.Save

    WScript.Echo "A shortcut to Notepad now exists on your Desktop."

    ' ********************************************************************************
    ' *
    ' * Welcome
    ' *
    Sub Welcome()
    Dim intDoIt

    intDoIt = MsgBox(L_Welcome_MsgBox_Message_Text, _
    vbOKCancel + vbInformation, _
    L_Welcome_MsgBox_Title_Text )
    If intDoIt = vbCancel Then
    WScript.Quit
    End If
    End Sub


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured