create shortcuts

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,060
I am trying to use the following script to create hundreds of shortcuts.

However I am getting a runtime error 5, invalid procedure

If I remove the TargetName from the line that errors and use "X:\Master Front Ends\TheBrain JT.mdb" it creates shortcuts, but obviously they would all have the same shortcut target/path which is not what I want

The target path is correct as when it places the TargetName code into the form textbox it matches a shortcut that I manually created and if I copy the code into a shortcut it works.

The path must be what I have stated as it is run from a terminal server and when we first set up a user we have to specify the msaccess.exe and work group join script and we find that when we have drop outs quite often users have to join the work group again, whereas when we use it in the path it automatically joins the group.

I am currently doing this in MS ACCESS, however once working, I will also use it in excel with a couple of minor tweaks.

I also posted the same question on the following.
Create Shortcuts - Access World Forums



Code:
Sub Create_Shortcuts()

' Requires a reference to the Windows Script Host Object model
     
    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

Dim YesNoCancel As VbMsgBoxResult

YesNoCancel = MsgBox("This code MUST be run from the terminal server, NOT from your PC ... Do you want to continue?", vbYesNoCancel + vbCritical, "Caution")
Select Case YesNoCancel

Case vbYes
GoTo StartScript1
Case vbNo
Exit Sub
Case vbCancel
Exit Sub
End Select


StartScript1:

If IsNull(Me.txtCopyDbase) Or Me.txtCopyDbase = "" Then
DoCmd.OpenForm "GeneralMessageBox"
Forms!GeneralMessageBox.lbCaption.Caption = vbNewLine & "You must select a database first"
Exit Sub
End If

Dim NewFileName As String, TargetName As String

'==================================================================================================
'Create shortcut loop based on initials/names in form listbox

Dim lngRow As Long
    Dim strMsg As String

    With Forms![F DB - switchboard]!EmpInitials
        For lngRow = 0 To .ListCount - 1
        
If Me.txtCopyDbase = "Brain" Then
NewFileName = "TheBrain " & .Column(0, lngRow) & ".lnk"
TargetName = """C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.EXE""" & " " & """X:\FrontEnds\User Databases\TheBrain " & .Column(0, lngRow) & ".mdb""" & " /wrkgrp " & """X:\KLIKTUBE.mdw"""
End If
        
        
'On Error GoTo Err2


' location to create shortcut in
    strShortcutPath = "X:\Master Front Ends\"
    
' name of shortcut
    strShortcutName = NewFileName
     
    Set objWSH = New IWshRuntimeLibrary.WshShell
    Set objShortCut = objWSH.CreateShortcut(strShortcutPath & strShortcutName)
    
'put link path into form textbox to make sure it is correct
    Me.txtlink = strShortcutToFile
    
'shortcut target path to file
    Me.txtlink = TargetName
  [B][COLOR=Red]  objShortCut.TargetPath = TargetName '"X:\Master Front Ends\TheBrain JT.mdb"[/COLOR][/B]
    'objShortCut.IconLocation = "C:\Program Files\Microsoft Office\Office12\MSN.ICO"
   
    objShortCut.Save
            
        Next lngRow
    End With
    
'==================================================================================================

    Set objShortCut = Nothing
    Set objWSH = Nothing
    
DoCmd.OpenForm "GeneralMessageBox"
Forms!GeneralMessageBox.lbCaption.Caption = vbNewLine & "Shortcuts created" & vbNewLine & ErrMessage
Exit Sub

Err1:
If Err = 70 Then
ErrMessage = ErrMessage & vbNewLine & fileName
Resume Next
End If

Err2:
If Err = 70 Then
DoCmd.OpenForm "GeneralMessageBox"
Forms!GeneralMessageBox.lbCaption.Caption = vbNewLine & "Either the database you are copying is open" & vbNewLine & "OR the existing file that you are replacing is open"
Exit Sub
End If
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,215,431
Messages
6,124,855
Members
449,194
Latest member
HellScout

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top