1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| Public Function createShortcutOnDesktop(Optional ByVal ShortcutName As String, Optional ByVal ShortcutPath As String, Optional ByVal TargetFullName As String) As Variant
On Error GoTo Catch 'plus bas ok
Dim WshShell As Object ' c'est quoi WshShell ?
Set WshShell = CreateObject("WScript.Shell") ' WScript.Shell 'c quoi le script ?
's'il n'y a pas de chemin, alors SpecialFolders("Desktop") ' SpecialFolders ?
If ShortcutPath = vbNullString Then
ShortcutPath = WshShell.SpecialFolders("Desktop")
createShortcutOnDesktop = -1 '-1 ? on fait koi là ?
Else
'si le chemin du dossier n'existe pas, alors on le crée
If Dir(ShortcutPath, vbDirectory) = vbNullString Then
createShortcutOnDesktop = "Le chemin de destination du raccourci n'est pas un chemin valide"
GoTo Catch
Else
createShortcutOnDesktop = -1
End If
End If
'si le nom du raccourci n'existe pas, alors on le nomme.
If ShortcutName = vbNullString Then
ShortcutName = "Réparation gest piéces V multipostes"
Else
' s'il y a un point à la fin du nom du raccourci, alors shortcutname = le nom avant le point
If Right$(ShortcutName, 1) = "." Then ShortcutName = Left$(ShortcutName, Len(ShortcutName) - 1)
End If
'si le chemin complet n'existe pas, alors on prend celui de thisworkbook
If TargetFullName = vbNullString Then
TargetFullName = ThisWorkbook.FullName
createShortcutOnDesktop = -1
Else
'si le chemin complet n'existe pas, on annonce
If Dir(TargetFullName, vbNormal) = vbNullString Then
createShortcutOnDesktop = "La cible du raccourci n'est pas une cible valide."
GoTo Catch
Else
createShortcutOnDesktop = -1
End If
End If
Dim MyShortcut As Object
'creation du raccourci. S'il y a un \ en 1er, on l'enlève, sinon on le met.
Set MyShortcut = WshShell.createShortcut(ShortcutPath & IIf(Right$(ShortcutPath, 1) = "\", vbNullString, "\") & ShortcutName & ".lnk")
With MyShortcut
'rajout de \\ au chemin complet
.TargetPath = TargetFullName & "\\"
.Save
End With
Catch:
If Err.Number > 0 Then
createShortcutOnDesktop = Err.Description ' késako ?
End If
'si wshshell existe alors on l'enléve
If Not WshShell Is Nothing Then Set WshShell = Nothing
'si le raccourci existe, on l'enleve
If Not MyShortcut Is Nothing Then Set MyShortcut = Nothing
End Function |
Partager