forked from mattretzer/Word-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoupdate Macro
288 lines (235 loc) · 12.6 KB
/
Autoupdate Macro
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
Option Explicit
Option Base 1
'''created by Matt Retzer - [email protected]
'''update by Erica Warren - [email protected]
'''version 1.0
'''last updated 1/27/2014: PC version works
'''will eventually be combined with attach template macro
Sub PCupdateCheck()
Dim dirNamePC As String
Dim dirNameBash As String
Dim dirNameMac As String
Dim logFileName As String
Dim logFilePC As String
Dim logFileMac As String
Dim currentUser As String 'could set for MAc & PC Does pre-binding matter?
Dim updateCheck As Boolean
Dim updateFrequency As Integer
Dim logString As String 'could set a few values for this up front
Dim lastModDate As Date
Dim cvUrl As String
Dim currentVersionST As String 'ST=StyleTemplate
Dim installedVersionST As String
Dim downloadLinkST As String
Dim localDrive As String
Dim templateFile As String
templateFile = "macmillan.dotm"
logFileName = "macmillan_macros.log"
updateCheck = True
updateFrequency = 1 'number of days between update checks
logString = ""
cvUrl = "https://macmillan.atlassian.net/wiki/display/PBL/Test"
installedVersionST = "(none installed)" 'default
''''''PCs
'''set PC vars
currentUser = Environ("USERNAME")
localDrive = Environ("PROGRAMDATA") & "\MacmillanStyleTemplate" 'use variable because not everyone has local drive set as C:
dirNamePC = localDrive & "\log"
logFilePC = dirNamePC & "\" & logFileName 'if we need to do this on C we can use currentUser
'''check if logfile exists
If Dir(logFilePC) <> vbNullString Then '''file exists
''get date modified of logfile
lastModDate = FileDateTime(logFilePC)
'''compare with current date - if not today
If DateDiff("d", lastModDate, Date) < updateFrequency Then
updateCheck = False
logString = Now & " -- updateCheck = " & updateCheck & ". Already checked today."
End If
Else ''''create directory if no exist '''file does not exist
'''Check if MacmillanStyleTemplate folder exists
If Dir(localDrive, vbDirectory) = vbNullString Then
MkDir (localDrive)
MkDir (dirNamePC)
logString = Now & " -- created MacmillanStyleTemplate and logfile "
Else ' Just create log folder
MkDir (dirNamePC)
logString = Now & " -- created logfile "
End If
End If
'''run log setup sub
LogInformation logFilePC, logString
If updateCheck = True Then 'toggle this to -True- to test further business
Exit Sub
Else ' updateCheck is still True
'Get version number of installed style template
Dim pcTemplatePath As String
If Dir(localDrive & "\" & templateFile) <> "" Then
pcTemplatePath = localDrive & "\" & templateFile
Documents.Open FileName:=pcTemplatePath, ReadOnly:=True, Visible:=False
installedVersionST = Documents(pcTemplatePath).CustomDocumentProperties("version")
Documents(pcTemplatePath).Close
Else
installedVersionST = 0
End If
'try to download current versions text file
Dim myURL As String
Dim WinHttpReq As Object
Dim oStream As Object
myURL = "https://macmillan.atlassian.net/wiki/download/attachments/9044274/Versions"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
On Error Resume Next
WinHttpReq.Send
' Exit sub if error in connecting to website
If Err.Number <> 0 Then 'request is not OK
LogInformation logFilePC, Now & " -- tried update; unable to connect to website"
Exit Sub
End If
If WinHttpReq.Status = 200 Then ' 200 = HTTP request is OK
'if connection OK, download file and save to log directory
myURL = WinHttpReq.responseBody
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile dirNamePC & "\versions.txt", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
Set oStream = Nothing
Set WinHttpReq = Nothing
End If
End If
'Get version number of current template
Dim g_strVar As String
g_strVar = ImportVariable(dirNamePC & "\versions.txt")
currentVersionST = g_strVar
'If installed = current
If installedVersionST >= currentVersionST Then
LogInformation logFilePC, Now & " -- current & installed versions match (current version is " & currentVersionST & "). "
Exit Sub
Else
'download the templates files
myURL = "https://macmillan.atlassian.net/wiki/download/attachments/9044274/macmillan.dotm"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send
If WinHttpReq.Status = 200 Then ' 200 = HTTP request is OK
myURL = WinHttpReq.responseBody
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile dirNamePC & "\macmillanNew.dotm", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
Set oStream = Nothing
Set WinHttpReq = Nothing
End If
'log the download.
LogInformation logFilePC, Now & " -- Downloaded new " & templateFile & " Style Template version: " & currentVersionST & " (Installed version was " & installedVersionST & ")"
'remove existing template file
'Check that file exists
If Dir(localDrive & "\" & templateFile) <> "" Then
Kill localDrive & "\" & templateFile
End If
'replace existing file
Name dirNamePC & "\macmillanNew.dotm" As localDrive & "\" & templateFile
LogInformation logFilePC, Now & " -- Replaced existing " & templateFile & " Style Template. Telling user to relaunch Word."
MsgBox "The Macmillan Style Template has been upgraded on your computer." & vbNewLine & vbNewLine & "Please RE-LAUNCH WORD for the updated Style Template to take effect!", , "ACTION REQUIRED"
End If
End Sub
Sub MacUpdateCheck()
'''separated into own sub. check if it still works -- Erica
Dim dirNamePC As String
Dim dirNameBash As String
Dim dirNameMac As String
Dim logFileName As String
Dim logFilePC As String
Dim logFileMac As String
Dim currentUser As String 'could set for MAc & PC Does pre-binding matter?
Dim updateCheck As Boolean
Dim updateFrequency As Integer
Dim logString As String 'could set a few values for this up front
Dim lastModDate As Date
Dim cvUrl As String
Dim currentVersionST As String 'ST=StyleTemplate
Dim installedVersionST As String
Dim downloadLinkST As String
Dim localDrive As String
logFileName = "macmillan_macros.log"
updateCheck = True
updateFrequency = 1 'number of days between update checks
logString = "created logfile: " & Now
cvUrl = "https://macmillan.atlassian.net/wiki/display/PBL/Test"
installedVersionST = "(none installed)" 'default
'''set Mac vars
'could do currentUSer via shell but OSA is a cleaner command
currentUser = MacScript("tell application " & Chr(34) & "System Events" & Chr(34) & Chr(13) & "return (name of current user)" & Chr(13) & "end tell")
dirNameBash = "/Users/" & currentUser & "/MacmillanStyleTemplate/test"
dirNameMac = "Macintosh HD:Users:" & currentUser & ":MacmillanStyleTemplate:test:"
logFileMac = dirNameMac & logFileName
'''check if logfile exists
If ShellAndWaitMac("[ -e " & dirNameBash & "/" & logFileName & " ] ; echo $?") = 0 Then '''file exists
'''get date modified of logfile
lastModDate = FileDateTime(logFileMac)
'''compare with current date - if not today
If DateDiff("d", lastModDate, Date) < updateFrequency Then
updateCheck = False
End If
logString = Date & " -- updateCheck = " & updateCheck
Else '''file does not exist
'''create directory if no exist
ShellAndWaitMac ("[ -e " & dirNameBash & " ] &>/dev/null || mkdir -p " & dirNameBash)
End If
'''run log setup sub
LogInformation logFileMac, logString
If updateCheck = False Then 'toggle this to -True- to test further business
Exit Sub
Else 'check the network, compare versions. Skipping domain since we are ooking at confluence, but would test ping hbpub.net
If ShellAndWaitMac("ping -o google.com &> /dev/null ; echo $?") <> 0 Then
LogInformation logFileMac, Date & " -- tried update; unable to connect to internet"
Exit Sub
Else
If ShellAndWaitMac("[ -e " & dirNameBash & "/macmillan.dotm ] ; echo $?") = 0 Then
currentVersionST = ShellAndWaitMac("curl -s " & cvUrl & " | grep -m 1 macmillan.dotm | awk -F'Version' '{print$2}' | awk '{print$1}'")
installedVersionST = ShellAndWaitMac("cp -rfp " & dirNameBash & "/macmillan.dotm /private/tmp/macmillan.zip ; unzip -qu /private/tmp/macmillan.zip -d /private/tmp ; cat /private/tmp/docProps/custom.xml | awk -F'vt:lpwstr' '{print $2}' | tr -d '<>/\\n'")
'MsgBox currentVersionST & ", " & installedVersionST
If currentVersionST = installedVersionST Then
LogInformation logFileMac, Date & " -- current & installed versions match (" & currentVersionST & "). Style Template already up to date"
updateCheck = False
Exit Sub
End If
Else
LogInformation logFileMac, Date & " -- Style Template not present, updateCheck = " & updateCheck
End If
End If
End If
'''download file to tmp
downloadLinkST = ShellAndWaitMac("curl -s " & cvUrl & " | grep -m 1 macmillan.dotm | awk '{print$2}' | sed -e 's/^.*wiki/wiki/' -e 's/?.*//'")
ShellAndWaitMac ("rm -f /private/tmp/macmillanNew.dotm ; curl -o /private/tmp/macmillanNew.dotm https://macmillan.atlassian.net/" & downloadLinkST)
LogInformation logFileMac, Date & " -- Downloaded new " & templateFile & " Style Template version: " & currentVersionST & ". (installed version was " & installedVersionST & ")"
'remove existing file: could Kill dirNameMac & "macmillan.dotm" But Using rm for one liner
ShellAndWaitMac ("rm -f " & dirNameBash & "/macmillan.dotm &>/dev/null")
'''replace existing file
Name "Macintosh HD:private:tmp:macmillanNew.dotm" As dirNameMac & "macmillan.dotm"
LogInformation logFileMac, "Replaced existing Style Template. Telling user to relaunch Word."
MsgBox "The Macmillan Style Template has been upgraded on your Mac." & vbNewLine & vbNewLine & "Please RE-LAUNCH WORD for the updated Style Template to take effect!", , "ACTION REQUIRED"
End Sub
Private Sub LogInformation(logFile As String, LogMessage As String)
Dim FileNum As Integer
FileNum = FreeFile ' next file number
Open logFile For Append As #FileNum ' creates the file if it doesn't exist
Print #FileNum, LogMessage ' write information at the end of the text file
Close #FileNum ' close the file
End Sub
Function ShellAndWaitMac(cmd As String) As String
Dim result As String
Dim scriptCmd As String ' Macscript command
'
scriptCmd = "do shell script """ & cmd & """"
result = MacScript(scriptCmd) ' result contains stdout, should you care
ShellAndWaitMac = result
End Function
Private Function ImportVariable(strFile As String) As String
Open strFile For Input As #1
Line Input #1, ImportVariable
Close #1
End Function