Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Shortcut Code (vb6)

    My splash screen runs the following code to create a shortcut:
    However, a user logged as e.g User6 or User4 it works fine.

    If, as in my laptop, the user logs in as Administrator on XP, the shortcut is not created .
    I ran some error checking and 0 was returned as error code, obviously without err.Description.
    Anybody any ideas ?

    <pre> Dim objShell As IWshShell_Class
    Dim objShortcut As IWshShortcut_Class
    Dim FolderItem As Variant
    Dim lsPath As String
    Dim lsShortCut As String
    Dim lsURL As String
    Dim lsConfigFileName As String
    Dim nType As Variant

    Set objShell = New IWshShell_Class

    lsShortCut = App.Title
    lsPath = App.Path & "" & App.EXEName

    For Each FolderItem In objShell.SpecialFolders
    If Mid(FolderItem, Len(FolderItem) - 6, 7) = "Desktop" And _
    InStr(1, FolderItem, "All Users") = 0 And _
    InStr(1, UCase(FolderItem), "ADMINISTRATOR") = 0 Then
    Set objShortcut = objShell.CreateShortcut(FolderItem & _
    "" & _
    lsShortCut & _
    ".lnk")
    objShortcut.TargetPath = lsPath
    objShortcut.Arguments = lsURL & " " & _
    lsConfigFileName & " " & _
    CStr(nType)
    objShortcut.IconLocation = "L:MMpdfutilitiesroldx01d.ico"

    objShortcut.Save
    End If
    Next</pre>


  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Shortcut Code (vb6)

    Your code specifically bypasses Administrator in And InStr(1, UCase(FolderItem), "ADMINISTRATOR") = 0. Is that not what you intended?
    Charlotte

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Shortcut Code (vb6)

    Charlotte, <img src=/S/igiveup.gif border=0 alt=igiveup width=31 height=23> I admit I never saw it.

    Thanks for the direction.

  4. #4
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    vancouver, BC, Br. Columbia
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Shortcut Code (vb6)

    Dave - I need to write code in VBA to create a shortcut to a user-specified file. What is IWshShell_Class and IWshShortcut_Class? Are they part of Windows? I can't find anything on this in the MS KNowledge Base.

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Shortcut Code (vb6)

    Have a look at this post <post#=36966>post 36966</post#>. It has links to the MSKB which should give you the clues. The Windows Script Host is what the WSH refers to.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Shortcut Code (vb6)

    Here is an example of creating desktop shortcut using WSH:

    Public Sub TestCreateDesktopShortcut()

    CreateDesktopShortcut ShortcutName:="New Shortcut", _
    TargetPath:="C:Program FilesMicrosoft OfficeOfficeWINWORD.EXE", _
    Args:=Chr$(34) & "Cocuments and SettingsMarkDMy DocumentsChandler.doc" & _
    Chr$(34) & " /m", _
    HotKey:="CTRL+SHIFT+C", _
    IconPath:="C:IconsMyIconsCCuteFTP.ico", _
    Description:="Test New Shortcut"
    ' Test msg:
    MsgBox "New desktop shortcut 'New Shortcut' successfully created.", _
    vbInformation, "SHORTCUT TEST"

    End Sub

    Public Sub CreateDesktopShortcut(ByRef ShortcutName As String, _
    ByRef TargetPath As String, _
    ByRef Args As String, _
    ByRef HotKey As String, _
    ByRef IconPath As String, _
    ByRef Description As String)

    Dim wsh As New IWshRuntimeLibrary.IWshShell_Class
    Dim shrtct As New IWshShortcut_Class
    Dim strDesktopPath As String

    strDesktopPath = wsh.SpecialFolders("Desktop")
    Set shrtct = wsh.CreateShortcut(strDesktopPath & "" & ShortcutName & ".lnk")

    With shrtct
    ' Delimit path with double-quotes (Asc 34) in case of spaces:
    .TargetPath = Chr$(34) & TargetPath & Chr$(34)
    .Arguments = Args 'delimit individual args if necessary (spaces)
    .HotKey = HotKey
    .IconLocation = IconPath
    .Description = Description
    .Save
    End With

    Set wsh = Nothing
    Set shrtct = Nothing

    End Sub

    Note that the TargetPath should be delimited by double-quotes (Chr$(34)) in event there are spaces in path. Likewise, individual arguments in the Args string (such as a file name with spaces in path) should likewise be delimited, as shown in example; multiple command-line args can be "strung" together this way. Running the test sub successfully created new desktop shortcut with the parameters & properties specified (view Shortcut Properties to verify). To use WSH in VB/VBA, set a reference to the Windows Script Host Object Model (IWshRuntimeLibrary type library). Typical path: C:WINDOWSSystem32wshom.ocx. For more info, check out WSH properties/methods in VBE Object Browser; also see MSKB 244677:

    HOWTO: Create a Desktop Shortcut with the Windows Script Host

    Also MSDN has a lot of stuff on using WSH. The examples are usually in VBScript, but should not be difficult to "translate" for use in VB/VBA:

    Windows Script Host - Managing Shortcuts

    HTH

  7. #7
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Shortcut Code (vb6)

    Attached is a VB project that includes a Class module that I use to create shortcuts. The code is very similar to MarkD's. I suppose it should be considering there is only basically one way to create shortcuts.

    Regards,
    Kevin Bell

Posting Permissions

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