-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelOpen.vbs
More file actions
50 lines (39 loc) · 1.68 KB
/
Copy pathExcelOpen.vbs
File metadata and controls
50 lines (39 loc) · 1.68 KB
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
Option Explicit
'--------------------------------------------------------------------------------
' ScriptName : ExcelOpen.vbs (was the extensionless file "ExcelOpen")
' Creator : Sungman Han
' Creation Date : 2019-09-16
' Description : Opens a workbook in a visible Excel instance and leaves it open,
' so that ReservationExcelEdit.vbs can attach to it afterwards.
'
' Usage : cscript //nologo ExcelOpen.vbs "C:\data\reservation.xlsx" [sheet]
' Output : the name of the selected worksheet
' Exit codes : 0 ok | 1 bad arguments | 2 file not found
'
' Note : Excel is deliberately NOT quit here. The instance is handed over
' to the next script in the chain, which is why the original files
' relied on GetObject(, "Excel.Application").
'--------------------------------------------------------------------------------
Dim vExcelPath, vExcel, vWorkbook, vSheet
If WScript.Arguments.Count < 1 Then
WScript.StdErr.WriteLine "Usage: cscript //nologo ExcelOpen.vbs <full path to workbook> [sheet name]"
WScript.Quit 1
End If
vExcelPath = WScript.Arguments.Item(0)
If Not CreateObject("Scripting.FileSystemObject").FileExists(vExcelPath) Then
WScript.StdErr.WriteLine "File not found: " & vExcelPath
WScript.Quit 2
End If
Set vExcel = CreateObject("Excel.Application")
vExcel.Visible = True
Set vWorkbook = vExcel.Workbooks.Open(vExcelPath)
If WScript.Arguments.Count >= 2 Then
Set vSheet = vWorkbook.Worksheets(WScript.Arguments.Item(1))
Else
Set vSheet = vWorkbook.Worksheets(1)
End If
WScript.StdOut.WriteLine vSheet.Name
Set vSheet = Nothing
Set vWorkbook = Nothing
Set vExcel = Nothing
WScript.Quit 0