# Programming a Shortcut with Visual Basic 2008 Express Edition.



## Popa Mintin (Jun 25, 2008)

Hi, 
Ive been struggling for some time to find how to program shortcuts using VB 2008 and placing them in an existing folder, (or non-existing for that matter). I have no problem making the actual shortcuts but I can only get the routine to work if I put the shortcuts on the desktop. Sadly, 300+ shortcuts on my (already crowded) desktop isnt really practical.

My current code is:-

Public Class Form1

' This is the VB4 DLL
Private Declare Function fCreateShellLink Lib "STKIT432.DLL" _
(ByVal lpstrFolderName As String, ByVal lpstrLinkName As String, _
ByVal lpstrLinkPath As String, ByVal lpstrLinkArguments As String) As Long

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

End ' This is just an abort if required.
End Sub
___________________________________________________________________________________________

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Ireturn As Long
Dim a As String = "..\..\Desktop" & Chr(0) ' Put S/c here.
Dim b As String = "Dave Sheriff - Alamo" & Chr(0) ' Name of C/s.
Dim c As String = "C:\music\~title artist\Alamo - Dave Sheriff.mp3" & Chr(0) ' This is the target file.
Dim d As String = "" & Chr(0) ' 4th required attribute.

Ireturn = fCreateShellLink(a, b, c, d)

End
End Sub

End Class
___________________________________________________________________________________________

Its String a thats giving me the problem, if I change it to an existing folder (say) C:\Test1 the shortcut fails. Any help would be appreciated.

Popa.


----------



## Popa Mintin (Jun 25, 2008)

bump


----------



## pvc_ (Feb 18, 2008)

may be you need to put a "\" at the end of the path. Or if that doesn't work, you can create the shortcut on the desktop and then move it to where you want using File.move()


----------



## Popa Mintin (Jun 25, 2008)

PVC, Thank you for replying, Your point one, I've tried all permutations of address that I could think of, including the \, and point two, as I said, "Sadly, 300+ shortcuts on my (already crowded) desktop isn't really practical". What I might've said was 300+ at a time…. But actually, some times it might only be 20 or so… but there's a lot I want to do with this approaching a thousand shortcuts probably. Maybe if I could find a way to produce the shortcut on the desktop and immediately move it to the required folder before processing the next shortcut… I've tried that too, but being new to VB 2008 I'm getting no joy with that either… I can't locate the shortcut on the desktop. Even then it's a bit inelegant isn't it ? At the end of the day I ought to be able to put the shortcut directly into any folder. I could highlight a whole folder of files and right click them and use the 'Make Shortcut' function to put them in a folder, and write a programme to change their names, but again that's not what programming is all about.

Popa


----------



## ClayC (Jul 25, 2008)

ok here I just find out your problem, well atleast for Visual Basic 2008 it's fixed, I have made the shortcut with the button click and stuff

you had a couple of things I had to change but it's fully working now, here is the code

------------------------------------------------

Function Create_ShortCut(ByVal TargetPath As String, ByVal ShortCutPath As String, ByVal ShortCutname As String, ByVal WorkPath As String, ByVal Window_Style As Integer, ByVal IconNum As Integer)
Dim VbsObj As Object
VbsObj = CreateObject("WScript.Shell")

Dim MyShortcut As Object
MyShortcut = VbsObj.CreateShortcut(ShortCutPath & "\" & ShortCutname & ".lnk")
MyShortcut.TargetPath = TargetPath
MyShortcut.WorkingDirectory = WorkPath
MyShortcut.WindowStyle = Window_Style
MyShortcut.IconLocation = TargetPath & "," & IconNum
MyShortcut.Save()

Return True
End Function

---------------------------------------------

Private Sub btnCreate_Click() Handles Button1.Click
'This will Create a ShortCut of Notepad in our desktop, its name is "Notepad", minimize windows when run, use the 2nd icon as the Shortcut icon.
Create_ShortCut("C:\WINDOWS\notepad.exe", "C:\", "Notepad", "C:\WINDOWS\", 1, 0)
End Sub



--------------------------------------------

few things to know, replace the window style to 1-3 all meaning this

1 - Normal Window
3 - Maximized Window
7 - Minimized Window

the Icon number is the index for windows to choose the icon from, normally programs use 0 to show the same icon as exe file, but if it's a program which have more then 1 icon in it, you can change to show other icons 

Hope it helps you like it helped me


----------



## Popa Mintin (Jun 25, 2008)

ClayC, 
Thank you so much for your reply, a week or so ago I discovered "WScript.Shell" and some of how to use it.
I have a utility up and running but your information is very revealing and it fills in a few gaps in the information I had.
I shall see if I can improve my little program.
Thank you again for your time and effort.

Popa Mintin


----------



## ClayC (Jul 25, 2008)

glad I could help ya out 

if you have any more problems post and pm me the thread link


----------

