Shortcut to folders

freeon

Registered User.
Local time
Yesterday, 20:44
Joined
Oct 29, 2018
Messages
17
I want to create a shortcut to a folder. Shortcut name is to be based on project name in textbox on a form called ProjectName. The shortcut location is to be based on name in a combo box on the same form called ComboEstimator. Code below does create the shortcut however it names the shortcut estimator + project name-shortcut. Not sure why it's adding estimator name to the shortcut name. And instead of creating the shortcut in folder with the same name of estimator it just creates the shortcut in P:\. I added "MsgBox Me.ComboEstimator.Column(1)" to the code and I can confirm that for the value returned there is a folder of the same name in P:\. I took this code from an excel workbook I have and tweaked it. Obviously I'm doing something wrong.

Code:
Private Sub Command247_Click()
  Dim fdObj As Object
  Dim objWSH As IWshRuntimeLibrary.WshShell
  Dim objShortCut As IWshRuntimeLibrary.WshShortcut
  Dim strPath  As String
  Dim strShortcutPath As String
  Dim strShortcutName As String
  Dim strShortcutToFile As String
  
  strShortcutPath = "P:\" & Me.ComboEstimator.Column(1)
  strShortcutName = (Me.ProjectName & "-" & "Shortcut.lnk")
  strShortcutToFile = "P:\Brsm Projects\Access\" & Me.ProjectName
  
  
  
  Set objWSH = New IWshRuntimeLibrary.WshShell
  Set objShortCut = objWSH.CreateShortcut(strShortcutPath & strShortcutName)
  Set fdObj = CreateObject("Scripting.FileSystemObject")
  
  
    objShortCut.TargetPath = strShortcutToFile
    objShortCut.Save
End Sub
 
Because you are telling it to ?

This line

Code:
Set objShortCut = objWSH.CreateShortcut([COLOR="Red"]strShortcutPath & strShortcutName)[/COLOR]

Concatenates the two strings you created here
Code:
 strShortcutPath = "P:\" & Me.ComboEstimator.Column(1)
 strShortcutName = (Me.ProjectName & "-" & "Shortcut.lnk")
 
So how do I fix it? I tried removing below and get error 91.

Code:
[FONT=&quot]
Set objShortCut = objWSH.CreateShortcut(strShortcutPath & strShortcutName)
[/FONT]
If I only remove "strShortcutPath & strShortcutName" I get "argument not optional

Code:
Set objShortCut = objWSH.CreateShortcut()
 
You need one of them. I would suggest from the description you need to use

Code:
Set objShortCut = objWSH.CreateShortcut(strShortcutName)
 
This is not working. It does nothing, doesn't create any shortcuts anywhere. If I [FONT=&quot]add " strShortcutPath & " back to the code it works, just wrong name and wrong location for the shortcut.[/FONT]

Code:
Private Sub Command247_Click()
  Dim fdObj As Object
  Dim objWSH As IWshRuntimeLibrary.WshShell
  Dim objShortCut As IWshRuntimeLibrary.WshShortcut
  Dim strPath  As String
  Dim strShortcutPath As String
  Dim strShortcutName As String
  Dim strShortcutToFile As String
  
  strShortcutName = (Me.ProjectName & "-" & "Shortcut.lnk")
  strShortcutToFile = "P:\Projects\Access\" & Me.ProjectName
  strShortcutPath = "P:\" & Me.ComboEstimator.Column(1)
  
  
  Set objWSH = New IWshRuntimeLibrary.WshShell
  Set objShortCut = objWSH.CreateShortcut(strShortcutName)
  Set fdObj = CreateObject("Scripting.FileSystemObject")
  
  
    objShortCut.TargetPath = strShortcutToFile
    objShortCut.Save
  

End Sub
 
Then I would try the other variable. Looking at it more closely it should be valid file path

Set objShortCut = objWSH.CreateShortcut(strShortcutPath )

The way you are setting this up a is a little confusing, Shortcut Path and Shortcut to File in my mind are the same thing?
 
As I mentioned before this is from excel workbook and was setup a bit differently in excel and obviously I'm doing something wrong when modifying to work in access. I am new to access and obviously missing something. Shortcut file and shortcut file are not the same thing. Or shouldn't be.

Shortcut to file= The folder the shortcut is pointing
Shortcut Path= Were the shortcut is located
 
Walk through the code step by step in the debugger.
Put a breakpoint on the first line of code
Code:
strShortcutName = (Me.ProjectName & "-" & "Shortcut.lnk")

by clicking to the left of the line and then use F8 to step through each line to see what you have.

You can hover over with the mouse or ? variable in the Immediate window to see what each contains.

That is how I always approach errors like these.

This is not working. It does nothing, doesn't create any shortcuts anywhere. If I [FONT=&quot]add " strShortcutPath & " back to the code it works, just wrong name and wrong location for the shortcut.[/FONT]

Code:
Private Sub Command247_Click()
  Dim fdObj As Object
  Dim objWSH As IWshRuntimeLibrary.WshShell
  Dim objShortCut As IWshRuntimeLibrary.WshShortcut
  Dim strPath  As String
  Dim strShortcutPath As String
  Dim strShortcutName As String
  Dim strShortcutToFile As String
  
  strShortcutName = (Me.ProjectName & "-" & "Shortcut.lnk")
  strShortcutToFile = "P:\Projects\Access\" & Me.ProjectName
  strShortcutPath = "P:\" & Me.ComboEstimator.Column(1)
  
  
  Set objWSH = New IWshRuntimeLibrary.WshShell
  Set objShortCut = objWSH.CreateShortcut(strShortcutName)
  Set fdObj = CreateObject("Scripting.FileSystemObject")
  
  
    objShortCut.TargetPath = strShortcutToFile
    objShortCut.Save
  

End Sub
 
I can't believe it was this simple. My original code works. Even with this "Set objShortCut = objWSH.CreateShortcut(strShortcutPath & strShortcutName)" it puts shortcut in correct folder and doesn't put estimator name in the shortcut name. If I remove "strShortcutPath" it doesn't work at all. All I had to do was add \ at the end.
Thank you for your replies

Code:
strShortcutPath = "P:\" & Me.ComboEstimator.Column(1) & "\"
 

Users who are viewing this thread

Back
Top Bottom