Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2002
    Posts
    10

    Post Unanswered: Generating Shortcut using Access VBA

    Hello...

    Can anyone suggest how to go about programatically generating a shortcut on the desktop...?

    Thanks in advance

    Mark

  2. #2
    Join Date
    Nov 2001
    Posts
    336
    Hi Mark,

    Here is some code you can adopt to your needs:
    It creates shortcut to Notepad.exe with name Notepad_999


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

    Dim MyShortcut As Object
    Dim DesktopPath As String

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

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

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

    Set MyShortcut = Nothing
    Set WSHShell = Nothing



    Igor

  3. #3
    Join Date
    Aug 2002
    Location
    Brazil - Sao Paulo
    Posts
    4
    Hello Igo,

    WSHShell is a OCX or DLL? what is reference name that I must to include in my VBA Environment?

    I'm waitting for your answer.

    Thanks!

  4. #4
    Join Date
    Nov 2001
    Posts
    336
    Since WSHShell declared in the script as an object you don't have to include additional references.

    Nevertheless, if you wish to include the reference, here you go:


    include wshom.ocx located in c:\windows\system\ folder.


    Igor

  5. #5
    Join Date
    Jul 2002
    Posts
    10
    Igor,

    the code suggested earlier worked fine,
    however, when attempting to include /wrkgrp switch,
    the target path in the newly created icon gets
    missinterpreted... (see modified code below)

    ...i.e. the /wrkgrp switch parameter is getting output as
    \wrkgrp which prevents the shortcut from opening the
    required file. ...any ideas?

    Thank you in advance

    Mark


    '________CODE___________________________

    Private Sub Command0_Click()
    Dim WSHShell As Object
    Set WSHShell = CreateObject("WScript.Shell")
    Dim AccessPath As String
    Dim MyShortcut As Object
    Dim DesktopPath As String
    Dim ClientDirectory As String
    Dim MdwSwitch As String
    Dim workfield As String
    Dim wv1 As Variant
    ' Read desktop path using WshSpecialFolders object
    DesktopPath = WSHShell.SpecialFolders("Desktop")
    AccessPath = """C:\Program Files\Microsoft Office\Office\MSACCESS.EXE"""
    ClientDirectory = """C:\My Documents\secure.mdb"""
    MdwSwitch = " " & "/" & "wrkgrp " & """C:\WINDOWS\system\system.mdw"""
    workfield = AccessPath & " " & ClientDirectory & MdwSwitch
    ' Create a shortcut object on the desktop
    Set MyShortcut = WSHShell.CreateShortcut(DesktopPath & "\Pasapp97.lnk")
    ' Set shortcut object properties and save it
    'MyShortcut.TargetPath = WSHShell.ExpandEnvironmentStrings("%windir%\notepa d.exe")

    'MyShortcut.TargetPath = WSHShell.ExpandEnvironmentStrings(workfield)
    MyShortcut.TargetPath = workfield
    'MyShortcut.WorkingDirectory = WSHShell.ExpandEnvironmentStrings("%windir%")
    MyShortcut.WorkingDirectory = WSHShell.ExpandEnvironmentStrings("C:\Program Files\Microsoft Office\Office\")
    MyShortcut.WindowStyle = 5
    MyShortcut.IconLocation = WSHShell.ExpandEnvironmentStrings("C:\Program Files\Microsoft Office\Office\MSACCESS.EXE, 0")
    'MyShortcut.IconLocation = WSHShell.ExpandEnvironmentStrings("%windir%\MSACCE SS.EXE, 0")
    MyShortcut.Save
    Set MyShortcut = Nothing
    Set WSHShell = Nothing
    End Sub
    '_________________________________________

  6. #6
    Join Date
    Jan 2009
    Posts
    1

    /wrkgrp switch parameter is getting output as

    has anyone figured this out yet?

  7. #7
    Join Date
    Apr 2004
    Location
    Sydney Australia
    Posts
    369
    Quote Originally Posted by nbdc321
    has anyone figured this out yet?
    IGelin has a couple of typos in the code as in

    notepa d

    change them to notepad and the shortcut will work

  8. #8
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    not typos.
    look at this:

    12345678901234567890123456789012345678901234567890 123456
    WSHShell.ExpandEnvironmentStrings("%windir%\notepa d.exe")

    there are zero typed spaces in that first line of numbers.
    this site inserts a space after 50 non-spaces (unless you use the CODE tags).

    interesting stuff IGelin - i could not persuade A to create a desktop shortcut with wsh but somehow made it work in Excel (makes no sense since wsh is the same). i'll have another go based on your version.

    izy
    currently using SS 2008R2

  9. #9
    Join Date
    Apr 2004
    Location
    Sydney Australia
    Posts
    369
    I copied and pasted that code behind a label and changed notepa d to notepad and it created a shortcut on the desktop and the shortcut worked

Posting Permissions

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