フォルダーやファイル等のショートカットの実体は、下図のプロパティの内容から作成されいます。あくまでもフォルダー・ファイル等本体では無いので、プロパティの設定でリンク先や作業フォルダー等を設定する必要があります。ショートカットのプロパティの内容については、下記の通りになります。
プロパティ | 説明 | 例 |
---|---|---|
TargetPath | リンク先のファイル名 | "C:\Users\akira\OneDrive\01_開発中” |
WorkingDirectory | 作業フォルダ | "C:\Users\akira\OneDrive\" |
Hotkey | キーボード・ショートカット | "Ctrl+Alt+Z" |
WindowStyle | 実行時の大きさ(ウィンドウのサイズ) | 1 '標準:1・最大化:3・最小化:7 |
Description | コメント(ショートカットの説明) | "ショートカットテストです。" |
IconLocation | ショートカットのアイコン | "%SystemRoot%\system32\SHELL32.dll, 41" 41は、木のアイコン |
Arguments | 実行ファイルに渡すパラメータ | |
FullName | (読み取り専用)ショートカットファイルのフルパス |
下記のサンプルプログラムは、デスクトップ上に指定したフォルダーのショートカットを作成するサンプルプログラムです。
【ショートカット】のプロパティについて
ショートカットを作成するにあたり、必須事項は、ショートカット名とリンク先(TargetPath)が必須事項となります。
' '******** AKIRA55.COM ******* https://akira55.com/shortcut/ ' Sub shortcut01() 'デスクトップ上にフォルダーのショートカットを作成する Dim WshShell As Object Dim DeskTopPath, ShortCutPath As String Set WshShell = CreateObject("WScript.Shell") 'WshShellオブジェクトを作成 DeskTopPath = WshShell.SpecialFolders("Desktop") 'デスクトップパスを取得する ShortCutPath = DeskTopPath & "\サンプルフォルダー" & ".lnk" 'ショートカット名の指定 With WshShell.CreateShortcut(ShortCutPath) .TargetPath = "C:\Users\akira\OneDrive\01_開発中\サンプルフォルダー" 'ショートカットのリンク先を設定 .WorkingDirectory = "C:\Users\akira\OneDrive\" '作業フォルダ .Hotkey = "Ctrl+Alt+Z" .WindowStyle = 1 '標準:1・最大化:3・最小化:7 .Description = "ショートカットテストです。" 'コメント欄の表記文字を設定 .IconLocation = "%SystemRoot%\system32\SHELL32.dll, 80" 'アイコンの設定 80番アイコン .Save 'ショートカットの保存実行 End With Set WshShell = Nothing End Sub '
' '******** AKIRA55.COM ******* https://akira55.com/shortcut/ ' Sub shortcut02() 'デスクトップ上にファイルのショートカットを作成する Dim WshShell As Object Dim DeskTopPath, ShortCutPath As String Set WshShell = CreateObject("WScript.Shell") 'WshShellオブジェクトを作成 DeskTopPath = WshShell.SpecialFolders("Desktop") 'デスクトップパスを取得する ShortCutPath = DeskTopPath & "\" & ThisWorkbook.Name & ".lnk" 'ショートカット名の指定(このブックのショートカット名) With WshShell.CreateShortcut(ShortCutPath) .TargetPath = ThisWorkbook.FullName 'ショートカットのリンク先を設定(このブック) .Save 'ショートカットの保存実行 End With Set WshShell = Nothing End Sub '
' '******** AKIRA55.COM ******* https://akira55.com/shortcut/ ' Sub shortcut03() 'デスクトップ上にインターネットURLのショートカットを作成する Dim WshShell As Object Dim DeskTopPath, ShortCutPath As String Set WshShell = CreateObject("WScript.Shell") 'WshShellオブジェクトを作成 DeskTopPath = WshShell.SpecialFolders("Desktop") 'デスクトップパスを取得する ShortCutPath = DeskTopPath & "\" & "Yahoo!JAPAN.URL" 'ショートカット名の指定(URLショートカット名) With WshShell.CreateShortcut(ShortCutPath) .TargetPath = "https://www.yahoo.co.jp/" 'ショートカットのリンク先を設定(YahooのURLを指定) .Save 'ショートカットの保存実行 End With Set WshShell = Nothing End Sub '
' '******** AKIRA55.COM ******* https://akira55.com/shortcut/ ' Sub shortcut04() 'デスクトップ上にペイント(アプリケーション)のショートカットを作成する Dim WshShell As Object Dim DeskTopPath, ShortCutPath As String Set WshShell = CreateObject("WScript.Shell") 'WshShellオブジェクトを作成 DeskTopPath = WshShell.SpecialFolders("Desktop") 'デスクトップパスを取得する ShortCutPath = DeskTopPath & "\" & "ペイント.lnk" 'ショートカット名の指定(ペイント) With WshShell.CreateShortcut(ShortCutPath) .TargetPath = "%windir%\system32\mspaint.exe" 'ショートカットのリンク先を設定(ペイントのEXEを指定) .Save 'ショートカットの保存実行 End With Set WshShell = Nothing End Sub '