Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I create shorcut using VBA? 1

Status
Not open for further replies.

wtstro

Programmer
Aug 1, 2002
15
US
I have an MS-Access application that uses a workgroup. Using VBA I want the user to click on a button that will create a shortcut to the application with the /wrkgrp parameter added to the target string (example: D:\MyProgram\MyAccessApp.mdb /wrkgrp d:\MyProgram\MyAccessSecurity.mdw)
[sadeyes] I have been looking for almost a week now. Can anyone help me???

 
You can use fCreateShellLink to create a shortcut. This example can be tweaked, but it creates program shortcuts to the desktop.
Code:
Option Explicit

Private Declare Function fCreateShellLink Lib "Vb5stkit.dll" (ByVal _
    lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal _
    lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long

Sub CreateShortcut(sDisplayName As String, sFullPath As String)
    Dim lReturn As Long
    Dim sDeskName As String
    Dim sLinkArgs As String
    On Error GoTo ErrHndlr
    'Add to Desktop
    sDeskName = "..\..\Desktop"
    sLinkArgs = ""
    lReturn = fCreateShellLink(sDeskName, sDisplayName, sFullPath, sLinkArgs)
    If lReturn <> 1 Then
        MsgBox &quot;There was an error creating the shortcut:&quot; & vbCrLf & sDisplayName & vbCrLf & sFullPath
    End If
    Exit Sub
ErrHndlr:
    sDeskName = &quot;System Error in CreateShortcut()!&quot;
    sDeskName = sDeskName & &quot;Error No: &quot; & Err.Number & vbCrLf & &quot;Error Desc: &quot; & Err.Description
    sDeskName = sDeskName & vbCrLf & sDisplayName & vbCrLf & sFullPath
    Err.Clear
    MsgBox sDeskName
End Sub

'Then, to Call it
Call CreateShortcut(&quot;SomeName&quot;, &quot;D:\SomeFolder\SomeFile.mdb&quot;)
Hope that helps...
 
Thanks do [thumbsup2] dsi for the suggestion above. I needed to be able to include the parameter to use an MS-Access workgroup that is being used by the database.

[wiggle] I played with this for quite some time the arrive at this solution:

Option Explicit

Public Declare Function fCreateShellLink Lib &quot;Vb5stkit.dll&quot; (ByVal _
lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal _
lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long


Sub DoShortcut()
Dim lReturn As Long
Dim DestPath As String
Dim LinkArg As String
Dim ShortcutName As String
Dim AppPath As String

AppPath = &quot;D:\Program Files\TSMN\&quot;
DestPath = &quot;C:\Program Files\Microsoft Office\Office\MSAccess.EXE&quot;
LinkArg = &quot; &quot; & Chr(34) & AppPath & &quot;TSMN.mdb&quot; & Chr(34) & &quot; /wrkgrp &quot; & Chr(34) & AppPath & &quot;TSMNSys.mdw&quot; & Chr(34)
ShortcutName = &quot;Shortcut to TSMN&quot;

'Add shortcut to desktop
lReturn = fCreateShellLink(&quot;..\..\Desktop&quot;, ShortcutName, DestPath, LinkArg)
If lReturn = 0 Then
Beep
MsgBox &quot;Error while creating Desktop shortcut for: &quot; & ShortcutName & &quot;.&quot;, vbCritical + vbOKOnly, &quot;Create Error&quot;
End If

'Add shortcut to Program Menu Group
lReturn = fCreateShellLink(&quot;&quot;, ShortcutName, DestPath, LinkArg)
If lReturn = 0 Then
Beep
MsgBox &quot;Error while creating Program Menu Group shortcut for: &quot; & ShortcutName & &quot;.&quot;, vbCritical + vbOKOnly, &quot;Create Error&quot;
End If

'Add shortcut to Startup Group
'Note that on Windows NT, the shortcut will not actually appear in the Startup group until your next reboot.
lReturn = fCreateShellLink(&quot;\Startup&quot;, ShortcutName, DestPath, LinkArg)
If lReturn = 0 Then
Beep
MsgBox &quot;Error while creating Startup Group shortcut for: &quot; & ShortcutName & &quot;.&quot;, vbCritical + vbOKOnly, &quot;Create Error&quot;
End If

End Sub


Call DoShortcut() from your code. It was important to set up the LinkArg variable using the Chr(34) in order to account for spaces within a folder name.
 
Thanks to dsi [thumbsup2] for the suggestion above. I needed to be able to include the parameter to use an MS-Access workgroup that is being used by the database.

[wiggle] I played with this for quite some time the arrive at this solution:

Option Explicit

Public Declare Function fCreateShellLink Lib &quot;Vb5stkit.dll&quot; (ByVal _
lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal _
lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long


Sub DoShortcut()
Dim lReturn As Long
Dim DestPath As String
Dim LinkArg As String
Dim ShortcutName As String
Dim AppPath As String

AppPath = &quot;D:\Program Files\TSMN\&quot;
DestPath = &quot;C:\Program Files\Microsoft Office\Office\MSAccess.EXE&quot;
LinkArg = &quot; &quot; & Chr(34) & AppPath & &quot;TSMN.mdb&quot; & Chr(34) & &quot; /wrkgrp &quot; & Chr(34) & AppPath & &quot;TSMNSys.mdw&quot; & Chr(34)
ShortcutName = &quot;Shortcut to TSMN&quot;

'Add shortcut to desktop
lReturn = fCreateShellLink(&quot;..\..\Desktop&quot;, ShortcutName, DestPath, LinkArg)
If lReturn = 0 Then
Beep
MsgBox &quot;Error while creating Desktop shortcut for: &quot; & ShortcutName & &quot;.&quot;, vbCritical + vbOKOnly, &quot;Create Error&quot;
End If

'Add shortcut to Program Menu Group
lReturn = fCreateShellLink(&quot;&quot;, ShortcutName, DestPath, LinkArg)
If lReturn = 0 Then
Beep
MsgBox &quot;Error while creating Program Menu Group shortcut for: &quot; & ShortcutName & &quot;.&quot;, vbCritical + vbOKOnly, &quot;Create Error&quot;
End If

'Add shortcut to Startup Group
'Note that on Windows NT, the shortcut will not actually appear in the Startup group until your next reboot.
lReturn = fCreateShellLink(&quot;\Startup&quot;, ShortcutName, DestPath, LinkArg)
If lReturn = 0 Then
Beep
MsgBox &quot;Error while creating Startup Group shortcut for: &quot; & ShortcutName & &quot;.&quot;, vbCritical + vbOKOnly, &quot;Create Error&quot;
End If

End Sub


Call DoShortcut() from your code. It was important to set up the LinkArg variable using the Chr(34) in order to account for spaces within a folder name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top