+ Reply to Thread
Results 1 to 4 of 4

Create VB shortcut to a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    02-19-2009
    Location
    Enschede NL
    MS-Off Ver
    Excel 2003
    Posts
    2

    Create VB shortcut to a folder

    Hello,

    I'm using Excel 2003 and want to programmaticly create shortcuts to folders. When I try the program below it works when the destiny is a file, but I need to open Windows Explorer with the shortcut.
    When using 'my' code a shortcut is placed in the right folder but when opening it a window pops up and asks with what application I want to open it, instead of Windows Explorer with the target folder.
    Looking at the properties of that shortcut it does say it's a folder-type.

    So, what is going wrong???

    Greetz, CJ.



    The shortcuts must be placed in one folder: C:\destiny\~shortcuts\
    The folders themselves are in: C:\customers\john doe\

    I've marked original with 'ORIGINAL and my work with 'MY EDIT


    Sub CreateShortcut() 
        Dim szMsg As String 
        Dim szStyle As String 
        Dim szTitle As String 
         
         
         ' Change here for the icon's name
        Const szIconName As String = "\cvg.ico" 
         
         ' Constant string values, you can replace "Desktop"
         ' with any Special Folders name to create the shortcut there
    'ORIGINAL
        Const szlocation As String = "Desktop" 
    ' MY EDIT
    'Const szlocation As String = "C:\destiny\~shortcuts\"
        Const szLinkExt As String = ".lnk" 
         
         
         ' Object variables
        Dim oWsh As Object 
        Dim oShortcut 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 
         
         ' Initialize variables
    'ORIGINAL
        szSep = Application.PathSeparator 
        szBookName = szSep & ThisWorkbook.Name 
        szBookFullName = ThisWorkbook.FullName 
        szPath = ThisWorkbook.Path 
    ' MY EDIT
    'szSep = Application.PathSeparator
    'szBookName = szSep & "customers\johndoe"
    'szBookFullName = "\customers\johndoe\"
    'szPath = "C:\customers\johndoe\"
         
        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") 
    'ORIGINAL
        szDesktopPath = oWsh.SpecialFolders(szlocation) 
    ' MY EDIT
    'szDesktopPath = szlocation
         
         ' Get the path where the shortcut will be located
        szShortcut = szDesktopPath & szBookName & szLinkExt 
         
         ' Make it happen
        Set oShortcut = oWsh.CreateShortCut(szShortcut) 
         
         ' Link it to this file
        With oShortcut 
            .TargetPath = szBookFullName 
            .IconLocation = szPath & szIconName 
            .Save 
        End With 
          
         ' Explicitly clear memory
        Set oWsh = Nothing 
        Set oShortcut = Nothing 
         
         ' Let the user know it was created ok
        szMsg = "Shortcut was created successfully" 
        szStyle = 0 
        szTitle = "Success!" 
        MsgBox szMsg, szStyle, szTitle 
         
        Exit Sub 
          
         ' or if it wasn't
    ErrHandle: 
        szMsg = "Shortcut could not be created" 
        szStyle = 48 
        szTitle = "Error!" 
         
        MsgBox szMsg, szStyle, szTitle 
    End Sub
    Last edited by CJPNL; 03-18-2010 at 11:28 AM.

  2. #2
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: Howto create a VB shortcut to a folder?

    Before a mod gets to you.... Can you please place [ code] [ /code] around your code in your post?

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Howto create a VB shortcut to a folder?

    Your szBookFullName appears to be missing a drive letter. (your code works fine for me)
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    02-19-2009
    Location
    Enschede NL
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Create VB shortcut to a folder

    Placed the [code] things and it does look much better!
    And yes, filling in the missing drive-letter solves the problem!

    Thanx.
    Last edited by CJPNL; 03-18-2010 at 11:28 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1