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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating A Shortcut

Status
Not open for further replies.

cpope

Programmer
Jul 7, 2000
58
US
Does anyone know a way to create a shortcut or a link through visual basic code. I have found some samples that work only with Windows '95, but it is really important that the code works with win95,98,NT4,2000.

Thanks!

Carey
 
1. Start a New Standard.Exe Project.
2. Add a ListBox (List1) to Form1.
3. Copy/Paste the following into the Form1 code window.
4. CHANGE the i_strShortcut variable to a valid Shortcut.
5. Press F5 to run. Click on VB Menu -> View -> Immediate Window and you will see all pertinent Shortcut information for the Link specified.

<----- Code begin ----->

Option Explicit

Private Sub Form_Load()

Dim strLinkSource As String
strLinkSource = &quot;c:\win95b\desktop\windows explorer.lnk&quot; ' <-- CHANGE
Dim strLinkTarget As String
strLinkTarget = &quot;c:\win95b\desktop\TEST.lnk&quot; ' <-- CHANGE

Call x_Link_Read(strLinkSource)
Call x_Link_Copy(strLinkSource, strLinkTarget)

End Sub

Private Sub x_Link_Copy(ByVal i_strSource As String, ByVal i_strTarget As String)

If Dir(i_strSource) = &quot;&quot; Then
MsgBox i_strSource & &quot; NOT FOUND&quot;, vbCritical
Exit Sub
End If

If Dir(i_strTarget) <> &quot;&quot; Then
MsgBox i_strTarget & &quot; ALREADY EXISTS&quot;, vbCritical
Exit Sub
End If

Dim wshShell As Object
Set wshShell = CreateObject(&quot;WScript.Shell&quot;)

Dim wshSource As Object
Set wshSource = wshShell.CreateShortcut(i_strSource)
With wshSource
Call x_Link_Write _
(i_strTarget, _
.Arguments, _
.Description, _
.Hotkey, _
.IconLocation, _
.TargetPath, _
.WindowStyle, _
.WorkingDirectory)
End With

Set wshShell = Nothing

End Sub

Private Sub x_Link_Read(ByVal i_strShortcut As String)

If Dir(i_strShortcut) = &quot;&quot; Then
MsgBox i_strShortcut & &quot; NOT FOUND&quot;, vbCritical
Exit Sub
End If

Dim wshShell As Object
Set wshShell = CreateObject(&quot;WScript.Shell&quot;)

Dim wshLink As Object
Set wshLink = wshShell.CreateShortcut(i_strShortcut)

List1.Clear
List1.AddItem UCase(i_strShortcut)
With wshLink
List1.AddItem &quot;Arguments: &quot; & .Arguments
List1.AddItem &quot;Description: &quot; & .Description
List1.AddItem &quot;FullName: &quot; & .FullName
List1.AddItem &quot;HotKey: &quot; & .Hotkey
List1.AddItem &quot;IconLocation: &quot; & .IconLocation
List1.AddItem &quot;TargetPath: &quot; & .TargetPath
List1.AddItem &quot;WindowStyle: &quot; & .WindowStyle
List1.AddItem &quot;WorkingDirectory: &quot; & .WorkingDirectory
End With

Set wshShell = Nothing

End Sub

Private Sub x_Link_Write _
(ByVal i_strShortcut As String, _
ByVal i_strArguments As String, _
ByVal i_strDescription As String, _
ByVal i_strHotkey As String, _
ByVal i_strIconLocation As String, _
ByVal i_strTargetPath As String, _
ByVal i_strWindowStyle As String, _
ByVal i_strWorkingDirectory As String)

Dim wshShell As Object
Set wshShell = CreateObject(&quot;WScript.Shell&quot;)

Dim wshLink As Object
Set wshLink = wshShell.CreateShortcut(i_strShortcut)

With wshLink
.Arguments = i_strArguments
.Description = i_strDescription
.Hotkey = i_strHotkey
.IconLocation = i_strIconLocation
.TargetPath = i_strTargetPath
.WindowStyle = i_strWindowStyle
.WorkingDirectory = i_strWorkingDirectory
End With

wshLink.Save

Set wshShell = Nothing

End Sub

<----- Code End ----->
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top