WshShortcut Object
프로그래밍 방식으로 바로 가기를 만들 수 있습니다.
* 도움말에 있는 코드라 모든 코드가 대동소이(大同小異)합니다.
Sub CreateShortcut()
Dim strDesktop As String
strDesktop = GetDesktopPath() ' 데스크탑 경로 가져오기
' 바로가기 생성 및 저장
CreateAndSaveShortcut strDesktop, ThisWorkbook.FullName, "CTRL+SHIFT+F", "notepad.exe, 0", "바로가기 스크립트"
End Sub
Function GetDesktopPath() As String
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
GetDesktopPath = WshShell.SpecialFolders("Desktop") ' 데스크탑 경로 반환
End Function
Sub CreateAndSaveShortcut(ByVal desktopPath As String, ByVal targetPath As String, ByVal hotkey As String, ByVal iconLocation As String, ByVal description As String)
Dim oShellLink As Object
Dim strShortcutPath As String
strShortcutPath = desktopPath & "\바로가기 스크립트.lnk" ' 바로가기 경로 정의
Set oShellLink = CreateShellLinkObject(strShortcutPath) ' 바로가기 객체 생성
With oShellLink
.targetPath = targetPath ' 대상 경로 설정
.WindowStyle = 1 ' 보통 크기로 열림
.hotkey = hotkey ' 단축키 설정 (CTRL+SHIFT+F)
.iconLocation = iconLocation ' 아이콘 위치 설정
.description = description ' 설명 설정
.WorkingDirectory = desktopPath ' 작업 디렉토리 설정
.Save ' 바로가기 저장
End With
End Sub
Function CreateShellLinkObject(ByVal shortcutPath As String) As Object
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
Set CreateShellLinkObject = WshShell.CreateShortcut(shortcutPath) ' 바로가기 객체 생성 반환
End Function
속성
Arguments Property
바로가기에 대한 인수를 설정하거나 바로가기의 인수를 식별합니다.
Syntax
object.Arguments
object : WScript 개체입니다.
주의
Arguments 속성은 문자열을 반환합니다.
Sub CreateShortcut()
Dim oShellLink As Object
Dim strDesktop As String
Set oShellLink = CreateShellLinkObject
If oShellLink Is Nothing Then Exit Sub
strDesktop = GetDesktopPath()
With oShellLink
.targetPath = ThisWorkbook.FullName
.WindowStyle = 1
.hotkey = "Ctrl+Alt+f"
.iconLocation = "notepad.exe, 0"
.description = "Shortcut Script"
.WorkingDirectory = strDesktop
.Arguments = "C:\myFile.txt"
.Save
End With
End Sub
Function GetDesktopPath() As String
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
GetDesktopPath = WshShell.SpecialFolders("Desktop")
End Function
Function CreateShellLinkObject() As Object
Dim WshShell As Object
Dim strDesktop As String
Dim oShellLink As Object
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
On Error GoTo 0
If WshShell Is Nothing Then
MsgBox "WScript.Shell 오브젝트를 생성할 수 없습니다."
Exit Function
Else
End If
strDesktop = GetDesktopPath()
Set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
Set CreateShellLinkObject = oShellLink
End Function
Description Property
바로 가기 설명을 반환합니다.
Syntax
object.Description
object : WshShortcut 개체입니다.
주의
Description 속성에는 바로 가기를 설명하는 문자열 값이 들어 있습니다.
예제
아래 예제는 Description 속성의 사용을 보여줍니다.
Sub CreateShortcut()
Dim WshShell As Object
Dim strDesktop As String
' WScript.Shell 객체 생성
Set WshShell = CreateObject("WScript.Shell")
' 데스크탑 경로 가져오기
strDesktop = WshShell.SpecialFolders("Desktop")
' 바로가기 생성 및 설정 함수 호출
CreateAndSaveShortcut WshShell, strDesktop, "Shortcut Script.lnk", WScript.ScriptFullName, 1, "Ctrl+Alt+e", "notepad.exe, 0", "Shortcut Script"
' URL 바로가기 생성 및 설정 함수 호출
CreateAndSaveURLShortcut WshShell, strDesktop, "Microsoft Web Site.url", "http://www.microsoft.com"
End Sub
Sub CreateAndSaveShortcut(ByVal WshShell As Object, ByVal desktopPath As String, ByVal shortcutName As String, ByVal targetPath As String, ByVal windowStyle As Integer, ByVal hotkey As String, ByVal iconLocation As String, ByVal description As String)
Dim oShellLink As Object
' 바로가기 객체 생성
Set oShellLink = WshShell.CreateShortcut(desktopPath & "\" & shortcutName)
With oShellLink
' 바로가기 속성 설정
.targetPath = targetPath
.windowStyle = windowStyle
.hotkey = hotkey
.iconLocation = iconLocation
.description = description
.WorkingDirectory = desktopPath
.Save
End With
End Sub
Sub CreateAndSaveURLShortcut(ByVal WshShell As Object, ByVal desktopPath As String, ByVal shortcutName As String, ByVal targetURL As String)
Dim oUrlLink As Object
' URL 바로가기 객체 생성
Set oUrlLink = WshShell.CreateShortcut(desktopPath & "\" & shortcutName)
With oUrlLink
' URL 바로가기 속성 설정
.targetPath = targetURL
.Save
End With
End Sub
FullName Property
바로 가기 개체 대상의 정식 경로를 반환합니다.
Syntax
object.FullName
object : WshShortcut 개체입니다.
Sub CreateShortcut()
Dim WshShell As Object
Dim strDesktop As String
Dim oShellLink As Object
' WScript.Shell 객체 생성
Set WshShell = CreateObject("WScript.Shell")
' 데스크탑 경로 가져오기
strDesktop = WshShell.SpecialFolders("Desktop")
' 바로가기 생성 및 설정
Set oShellLink = CreateAndSaveShortcut(WshShell, strDesktop, "Shortcut Script.lnk", ThisWorkbook.FullName, 1, "CTRL+SHIFT+F", "notepad.exe, 0", "Shortcut Script")
If Not oShellLink Is Nothing Then
' 생성된 바로가기의 경로 출력
MsgBox oShellLink.FullName
End If
End Sub
Function CreateAndSaveShortcut(ByVal WshShell As Object, ByVal desktopPath As String, ByVal shortcutName As String, ByVal targetPath As String, ByVal windowStyle As Integer, ByVal hotkey As String, ByVal iconLocation As String, ByVal description As String) As Object
Dim oShellLink As Object
' 바로가기 객체 생성
Set oShellLink = WshShell.CreateShortcut(desktopPath & "\" & shortcutName)
If oShellLink Is Nothing Then
MsgBox "바로가기 객체를 생성할 수 없습니다."
Exit Function
End If
With oShellLink
' 바로가기 속성 설정
.targetPath = targetPath
.windowStyle = windowStyle
.hotkey = hotkey
.iconLocation = iconLocation
.description = description
.WorkingDirectory = desktopPath
.Save
End With
' 생성한 바로가기 객체 반환
Set CreateAndSaveShortcut = oShellLink
End Function
Hotkey Property
바로 가기에 키 조합을 지정하거나 바로 가기에 지정된 키 조합을 식별합니다.
Syntax
object.Hotkey = strHotkey
object : WshShortcut 개체입니다.
strHotkey : 바로 가기에 지정된 키 조합을 나타내는 문자열입니다.
strHotkey의 구문은 아래와 같습니다.
[KeyModifier]KeyName
KeyModifier Alt+, Ctrl+, Shift+, Ext+ 중 하나일 수 있습니다.
참고 Ext+는 "확장된 키"를 의미합니다. 이것은 Shift 키의 새 유형이 앞으로 문자 집합에 추가되는 경우에 나타납니다.
KeyName a ... z, 0 ... 9, F1 ... F12, ...
KeyName은 대/소문자를 구분하지 않습니다.
주의
바로 가기 키는 연결된 모든 키를 동시에 누를 때 바로 가기를 시작하는 키 조합입니다.
바로 가기 키는 시스템의 바탕 화면 및 Windows 시작 메뉴에 있는 바로 가기를 시작하는 데 사용될 수 있습니다.
참고
바로 가기 키에 대한 다른 이름은 키보드 바로 가기 입니다.
아래 예제는 HotKey 속성의 사용을 보여줍니다.
Sub CreateShortcut()
Dim strDesktop As String
strDesktop = GetDesktopPath()
Dim oShellLink As Object
Set oShellLink = CreateShellLinkObject(strDesktop & "\Shortcut Script.lnk")
ConfigureShellLink oShellLink, ThisWorkbook.FullName, 1, "Ctrl+Alt+e", "notepad.exe, 0", "Shortcut Script"
SaveShellLink oShellLink
End Sub
Function GetDesktopPath() As String
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
GetDesktopPath = WshShell.SpecialFolders("Desktop")
End Function
Function CreateShellLinkObject(ByVal shortcutPath As String) As Object
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
On Error Resume Next
Set CreateShellLinkObject = WshShell.CreateShortcut(shortcutPath)
On Error GoTo 0
End Function
Sub ConfigureShellLink(ByRef oShellLink As Object, ByVal targetPath As String, ByVal windowStyle As Integer, ByVal hotkey As String, ByVal iconLocation As String, ByVal description As String)
With oShellLink
.targetPath = targetPath
.windowStyle = windowStyle
.hotkey = hotkey
.iconLocation = iconLocation
.description = description
End With
End Sub
Sub SaveShellLink(ByRef oShellLink As Object)
If Not oShellLink Is Nothing Then
oShellLink.Save
End If
End Sub
IconLocation Property
바로 가기에 아이콘을 지정하거나 바로 가기에 지정된 아이콘을 식별합니다.
Syntax
object.IconLocation = strIconLocation
object : WshShortcut 개체입니다.
strIconLocation : 아이콘을 찾는 문자열입니다. 문자열에는 정식 경로와 아이콘과 연결된 인덱스가 들어 있어야 합니다.
주의
문자열입니다.
아래 예제는 IconLocation 속성의 사용을 보여줍니다.
Sub CreateShortcut()
Dim strDesktop As String
strDesktop = GetDesktopPath()
Dim oShellLink As Object
Set oShellLink = CreateShellLinkObject(strDesktop & "\Shortcut Script.lnk")
ConfigureShellLink oShellLink, ThisWorkbook.FullName, 1, "Ctrl+Alt+e", "notepad.exe, 0", "Shortcut Script"
SaveShellLink oShellLink
End Sub
Function GetDesktopPath() As String
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
GetDesktopPath = WshShell.SpecialFolders("Desktop")
End Function
Function CreateShellLinkObject(ByVal shortcutPath As String) As Object
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
On Error Resume Next
Set CreateShellLinkObject = WshShell.CreateShortcut(shortcutPath)
On Error GoTo 0
End Function
Sub ConfigureShellLink(ByRef oShellLink As Object, ByVal targetPath As String, ByVal windowStyle As Integer, ByVal hotkey As String, ByVal iconLocation As String, ByVal description As String)
With oShellLink
.targetPath = targetPath
.windowStyle = windowStyle
.hotkey = hotkey
.iconLocation = iconLocation
.description = description
End With
End Sub
Sub SaveShellLink(ByRef oShellLink As Object)
If Not oShellLink Is Nothing Then
oShellLink.Save
End If
End Sub
RelativePath Property
바로 가기에 상대 경로를 지정하거나 바로 가기의 상대 경로를 식별합니다.
Syntax
object.RelativePath
object : WshShortcut 개체입니다.
아래 코드는 바로 가기의 상대 경로를 설정합니다.
Sub CreateShortcut()
Dim WshShell As Object
Dim WshShortcut As Object
Set WshShell = CreateObject("WScript.Shell")
Set WshShortcut = WshShell.CreateShortcut("MyScript.lnk")
WshShortcut.RelativePath = "C:\Scripts\"
End Sub
TargetPath Property
바로 가기 개체의 실행 파일 경로입니다.
Syntax
object.TargetPath
object : WshShortcut 또는 WshUrlShortcut 개체입니다.
주의
문자열입니다.
이 속성은 바로 가기의 대상 경로만을 위한 것입니다. 바로 가기의 모든 인수는 Arguments 속성에 들어 있어야 합니다.
아래 예제는 TargetPath 속성의 사용을 보여줍니다.
Sub CreateShortcuts()
Dim WshShell As Object
Dim strDesktop As String
Dim oShellLink As Object
Dim oUrlLink As Object
' WScript.Shell 객체 생성
Set WshShell = CreateObject("WScript.Shell")
' 데스크탑 경로 가져오기
strDesktop = WshShell.SpecialFolders("Desktop")
' 바로가기 생성 및 설정
Set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
With oShellLink
.TargetPath = ThisWorkbook.FullName
.WindowStyle = 1
.Hotkey = "Ctrl+Alt+e"
.IconLocation = "notepad.exe, 0"
.Description = "Shortcut Script"
.WorkingDirectory = strDesktop
.Save
End With
' URL 바로가기 생성 및 설정
Set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
With oUrlLink
.TargetPath = "http://www.microsoft.com"
.Save
End With
End Sub
기능별로 나눈 코드입니다.
Sub CreateShortcuts()
Dim WshShell As Object
Dim strDesktop As String
' WScript.Shell 객체 생성
Set WshShell = CreateObject("WScript.Shell")
' 데스크탑 경로 가져오기
strDesktop = WshShell.SpecialFolders("Desktop")
' 바로가기 생성 및 설정
CreateAndSaveShortcut WshShell, strDesktop, "Shortcut Script.lnk", ThisWorkbook.FullName, 1, "Ctrl+Alt+e", "notepad.exe, 0", "Shortcut Script"
' URL 바로가기 생성 및 설정
CreateAndSaveURLShortcut WshShell, strDesktop, "Microsoft Web Site.url", "http://www.microsoft.com"
End Sub
Sub CreateAndSaveShortcut(ByVal WshShell As Object, ByVal desktopPath As String, ByVal shortcutName As String, ByVal targetPath As String, ByVal windowStyle As Integer, ByVal hotkey As String, ByVal iconLocation As String, ByVal description As String)
Dim oShellLink As Object
' 바로가기 객체 생성
Set oShellLink = WshShell.CreateShortcut(desktopPath & "\" & shortcutName)
' 바로가기 속성 설정
With oShellLink
.targetPath = targetPath
.windowStyle = windowStyle
.hotkey = hotkey
.iconLocation = iconLocation
.description = description
.WorkingDirectory = desktopPath
.Save
End With
End Sub
Sub CreateAndSaveURLShortcut(ByVal WshShell As Object, ByVal desktopPath As String, ByVal shortcutName As String, ByVal targetURL As String)
Dim oUrlLink As Object
' URL 바로가기 객체 생성
Set oUrlLink = WshShell.CreateShortcut(desktopPath & "\" & shortcutName)
' URL 바로가기 속성 설정
With oUrlLink
.targetPath = targetURL
.Save
End With
End Sub
WindowStyle Property
바로 가기에 창 스타일을 지정하거나 바로 가기에서 사용하는 창 스타일을 식별합니다.
object.WindowStyle = intWindowStyle
object : WshShortcut 개체입니다.
intWindowStyle : 실행 중인 프로그램의 창 스타일을 설정합니다.
WindowStyle 속성은 정수를 반환합니다.
다음 표는 intWindowStyle에 사용할 수 있는 설정을 나열한 것입니다.
Sub CreateShortcuts()
Dim WshShell As Object
Dim strDesktop As String
' WScript.Shell 객체 생성
Set WshShell = CreateObject("WScript.Shell")
' 데스크탑 경로 가져오기
strDesktop = WshShell.SpecialFolders("Desktop")
' 바로가기 생성 및 설정
CreateAndSaveShortcut WshShell, strDesktop, "Shortcut Script.lnk", ThisWorkbook.FullName, 2, "Ctrl+Alt+e", "notepad.exe, 0", "Shortcut Script"
' URL 바로가기 생성 및 설정
CreateAndSaveURLShortcut WshShell, strDesktop, "Microsoft Web Site.url", "http://www.microsoft.com"
End Sub
Sub CreateAndSaveShortcut(ByVal WshShell As Object, ByVal desktopPath As String, ByVal shortcutName As String, ByVal targetPath As String, ByVal windowStyle As Integer, ByVal hotkey As String, ByVal iconLocation As String, ByVal description As String)
Dim oShellLink As Object
' 바로가기 객체 생성
Set oShellLink = WshShell.CreateShortcut(desktopPath & "\" & shortcutName)
' 바로가기 속성 설정
With oShellLink
.targetPath = targetPath
.windowStyle = windowStyle
.hotkey = hotkey
.iconLocation = iconLocation
.description = description
.WorkingDirectory = desktopPath
.Save
End With
End Sub
Sub CreateAndSaveURLShortcut(ByVal WshShell As Object, ByVal desktopPath As String, ByVal shortcutName As String, ByVal targetURL As String)
Dim oUrlLink As Object
' URL 바로가기 객체 생성
Set oUrlLink = WshShell.CreateShortcut(desktopPath & "\" & shortcutName)
' URL 바로가기 속성 설정
With oUrlLink
.targetPath = targetURL
.Save
End With
End Sub
WorkingDirectory Property
바로 가기에 작업 디렉터리를 지정하거나 바로 가기에서 사용하는 작업 디렉터리를 식별합니다.
Syntax
object.WorkingDirectory = strWorkingDirectory
object : WshShortcut 개체입니다.
strWorkingDirectory : 문자열입니다. 바로 가기가 시작하는 디렉터리입니다.
주의
문자열입니다.
아래 예제는 WorkingDirectory 속성의 사용을 보여줍니다.
Function InitShell() As Object
' WScript.Shell 객체 생성
Set InitShell = CreateObject("WScript.Shell")
End Function
Function GetDesktopPath(WshShell As Object) As String
' 데스크탑 경로 가져오기
GetDesktopPath = WshShell.SpecialFolders("Desktop")
End Function
Sub CreateFileShortcut(WshShell As Object, strDesktop As String)
Dim oShellLink As Object
' 바로가기 생성 및 설정
Set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
With oShellLink
.targetPath = Application.ActiveWorkbook.FullName ' 현재 열린 워크북의 전체 경로를 타겟으로 설정합니다.
.windowStyle = 1 ' 창 스타일을 "정상"으로 설정합니다.
.hotkey = "Ctrl+Alt+e" ' 단축키를 Ctrl + Alt + e 로 설정합니다.
.iconLocation = "notepad.exe, 0" ' 아이콘 위치를 notepad.exe의 첫 번째 아이콘으로 설정합니다.
.description = "Shortcut Script" ' 설명을 Shortcut Script 로 설정합니다.
.WorkingDirectory = strDesktop ' 작업 디렉터리를 사용자의 데스크탑으로 설정합니다.
.Save ' 변경 사항을 저장하고 바로가기를 만듭니다.
End With
End Sub
Sub CreateUrlShortcut(WshShell As Object, strDesktop As String)
Dim oUrlLink As Object
Set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
oUrlLink.targetPath = "http://www.microsoft.com" ' 타겟 URL을 Microsoft 홈페이지로 설정합니다.
oUrlLink.Save ' 변경 사항을 저장하고 URL 바로가기를 만듭니다.
End Sub
Sub Main()
Dim WshShell As Object
Dim strDesktop As String
Set WshShell = InitShell()
strDesktop = GetDesktopPath(WshShell)
CreateFileShortcut WshShell, strDesktop
CreateUrlShortcut WshShell, strDesktop
End Sub
메서드
Save Method
바로 가기 개체를 디스크에 저장합니다.
Syntax
object.Save
object : WshShortcut 또는 WshUrlShortcut 개체입니다.
주의
CreateShortcut 메서드를 사용하여 바로 가기 개체를 만들고 바로 가기 개체의 속성을 설정한 후 Save 메서드를 사용하여 디스크에 바로 가기 개체를 저장해야 합니다. Save 메서드는 바로 가기 개체의 FullName 속성에 있는 정보를 사용하여 바로 가기 개체를 디스크 어디에 저장할 것인지 결정합니다. 프린터 링크나 예약된 작업을 제외하고 파일, 디렉터리 및 드라이브와 같은 시스템 개체의 바로 가기만 만들 수 있습니다.
아래 예제는 Save 메서드의 사용을 보여줍니다.
Function CreateWshShell() As Object
Set CreateWshShell = CreateObject("WScript.Shell")
End Function
Function GetDesktopPath(wshShell As Object) As String
GetDesktopPath = wshShell.SpecialFolders("Desktop")
End Function
Sub CreateAndSaveShortcut(wshShell As Object, targetPath As String, hotkey As String, iconLocation As String)
Dim strDesktop As String
Dim oShellLink As Object
' 데스크탑 경로 가져오기
strDesktop = GetDesktopPath(wshShell)
' 바로가기 생성 및 설정
Set oShellLink = wshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
With oShellLink
.targetPath = targetPath
.windowStyle = 1
.hotkey = hotkey
.iconLocation = iconLocation
.description = "Shortcut Script"
.WorkingDirectory = strDesktop
.Save
End With
End Sub
Sub CreateAndSaveUrlLink(wshShell As Object, targetURL As String)
Dim strDesktop As String
Dim oUrlLink As Object
' 데스크탑 경로 가져오기
strDesktop = GetDesktopPath(wshShell)
' URL 바로가기 생성 및 설정
Set oUrlLink = wshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
oUrlLink.targetPath = targetURL
oUrlLink.Save
End Sub
Sub RunModule()
Dim wshShell As Object
Set wshShell = CreateWshShell
' 바로가기 생성 및 설정 실행
CreateAndSaveShortcut wshShell, ThisWorkbook.FullName, "Ctrl+Shift+F", "notepad.exe, 0"
' URL 바로가기 생성 및 설정 실행
CreateAndSaveUrlLink wshShell, "http://www.microsoft.com"
End Sub
도움말 출처
Windows Script Host
Table of contents Windows Script Host Article 07/17/2015 In this article --> The following sections provide information about Windows Script Host along with a reference section that documents the object model. In This Section
learn.microsoft.com
'Windows Script Host' 카테고리의 다른 글
WshNetwork 개체 (0) | 2023.08.17 |
---|---|
WshEnvironment 개체 (0) | 2023.08.17 |
WshShell 개체 (0) | 2023.08.15 |