-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlrun.bas
134 lines (113 loc) · 4.12 KB
/
xlrun.bas
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
Attribute VB_Name = "xlrun"
' To Save in %AppData%\Microsoft\AddIns to load automatically
' ThisWorkbook.Workbook_Open will call XlRun routine below.
Option Explicit
Sub XlRun()
' Read XLRUN and XLRUN_OUT environment varibale
' - XLRUN should contain command formated as : -xlFileOpen MyWorkbook.xlsx -xlRefreshLeftToRight -xlRngGet Summary!TestStatus
' - XLRUN_OUT should contain the output file (default C:\temp\xlrun.out)
Dim tokens() As String
If Environ("XLRUN") = "" Then
' Nothing to do
'Exit Sub
tokens = Split("-xlFileOpen MyWorkbook.xlsx -xlRefreshLeftToRight -xlRngGet Summary!TestStatus ", " ")
tokens = Split("-xlFileOpen MyMacrobook.xlsm -xlEvalMacro MyMacro -xlRngGet Summary!B4 -xlFileSave", " ")
tokens = Split("-xlFileNew -xlRngSet A1 1.0 -xlRngGet A1 -xlRngSet A2 =today() -xlRngGet A2 -xlFileSaveAs MyGeneratedBook.xlsx", " ")
Else
tokens = Split(Environ("XLRUN"), " ")
End If
Dim fout_path As String, fout
fout_path = Environ("XLRUN_OUT")
If fout_path = "" Then fout_path = "C:\temp\xlrun.out"
If fout_path <> "" Then
' Pure VBA
' fout = FreeFile
' Open Environ("XLRUN_OUT") For Output As #fout
' Write #fout, "xlRun"
' Using COM
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(fout_path, True)
Call fout.WriteLine("xlrun")
End If
Dim i As Long, token As String, cmd As String
For i = LBound(tokens) To UBound(tokens)
If Left(tokens(i), 1) = "-" Then
cmd = Mid(tokens(i), 2)
If cmd = "xlFileOpen" Or cmd = "xlFilePath" Then
Call xlFileOpen(fout, tokens(i + 1))
ElseIf cmd = "xlFileNew" Then
Call xlFileNew(fout)
ElseIf cmd = "xlFileSave" Then
Call xlFileSave(fout)
ElseIf cmd = "xlFileSaveAs" Then
Call xlFileSaveAs(fout, tokens(i + 1))
ElseIf cmd = "xlRefreshLeftToRight" Then
Call xlRefreshLeftToRight(fout)
ElseIf cmd = "xlEvalMacro" Then
Call xlEvalMacro(fout, tokens(i + 1))
ElseIf cmd = "xlRngSet" Then
Call xlRngSet(fout, tokens(i + 1), tokens(i + 2))
ElseIf cmd = "xlRngGet" Then
Call xlRngGet(fout, tokens(i + 1))
Else
Debug.Assert False
End If
End If
Next i
ActiveWorkbook.Close False
PrintOut fout, "Done"
' If fout > 0 Then Close #fout
Call fout.Close
End Sub
Function PrintOut(fout, msg As String)
Debug.Print msg
' If fout > 0 Then Write #fout, msg
fout.WriteLine msg
End Function
Function xlFileOpen(fout, xlpath As String)
PrintOut fout, "xlFileOpen: " & xlpath
Dim wbk As Workbook
Set wbk = Workbooks.Open(xlpath)
wbk.Activate
End Function
Function xlFileNew(fout)
PrintOut fout, "xlFileNew"
Dim wbk As Workbook
Set wbk = Workbooks.Add
wbk.Activate
End Function
Function xlFileSave(fout)
PrintOut fout, "xlFileSave"
Dim wbk As Workbook
Set wbk = ActiveWorkbook
wbk.Save
End Function
Function xlFileSaveAs(fout, xlpath As String)
PrintOut fout, "xlFileSaveAs: " & xlpath
Dim wbk As Workbook
Set wbk = ActiveWorkbook
Call wbk.SaveAs(xlpath)
End Function
Function xlRefreshLeftToRight(fout)
Dim wsh As Worksheet
For Each wsh In ActiveWorkbook.Worksheets
PrintOut fout, "xlRefreshLeftToRight: " & wsh.Name
wsh.Select
wsh.Activate
Next wsh
End Function
Function xlEvalMacro(fout, macroName As String)
PrintOut fout, "xlEvalMacro: " & macroName
Call Application.Run(macroName)
End Function
Function xlRngSet(fout, rngName As String, rngValue)
PrintOut fout, "xlRngSet: " & rngName & " = " & rngValue
Range(rngName) = rngValue
End Function
Function xlRngGet(fout, rngName As String)
Dim rngValue
rngValue = Range(rngName)
PrintOut fout, "xlRngGet: " & rngName & " = " & rngValue
' to write to a file
End Function