Discussion Locked

Create Shortcut by Coding

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Can u please guide me how to create a shortcuts referrence at the Desktop using Visual Basic 6 coding ..??

5 replies

by sl-campbell

'Add this to the declarations section of the form or module,
'(In the same form or module where the below AddShortCutToDesktop sub is placed).

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

'Then, create this sub in the same form or module
Public Sub AddShortCutToDesktop(sLinkPathAndFile As String)
'sLinkPathAndFile is the file path and name: "C:\MYPATH\MYFILE"

Dim lReturn As Long
Dim sFolderName As String
Dim sLinkName As String
Dim sLinkArgs As String

On Error GoTo ErrHandler

'(Here you could also use an API call to get the Windows Desktop folder)
sDeskName = "..\..\Desktop"

sLinkDesc = "Shortcut to MYFILE"
sLinkArgs = vbNullString

lReturn = fCreateShellLink(sDeskName, sLinkDesc, sLinkPathAndFile, sLinkArgs)

If lReturn <> 1 Then GoTo ErrHandler

ExitProceedure:
Exit Sub

ErrHandler:
MsgBox "Error in Adding desktop short cut", vbOKCancel Or vbCritical, "Error in AddShortCutToDesktop"
On Error GoTo 0
End Sub

'Please note: This is only a "quick fix", and it would be better to ask Windows via an API call where theDesktop folder is. Also you may want to add some type of file checking to make sure you are passing a true path and file name to the sub.

This discussion is closed to new replies.