• Shortcut Code (vb6)

    • This topic has 12 replies, 6 voices, and was last updated 21 years ago.
    Author
    Topic
    #402357

    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 ?

        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
    Viewing 3 reply threads
    Author
    Replies
    • #799699

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

    • #799700

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

    • #826960

      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.

      • #826979

        Have a look at this post post 36966. It has links to the MSKB which should give you the clues. The Windows Script Host is what the WSH refers to.

      • #826980

        Have a look at this post post 36966. It has links to the MSKB which should give you the clues. The Windows Script Host is what the WSH refers to.

      • #826982

        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) & “C:Documents 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

        • #827111

          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

        • #827112

          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

      • #826983

        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) & “C:Documents 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

    • #826961

      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.

    Viewing 3 reply threads
    Reply To: Shortcut Code (vb6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: