strComputer = "."
dim gExcel
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WSHShell = wscript.CreateObject("wscript.shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
oldfile = "Test_AddIn (Ver1.5).xla"
newfile = "Test_AddIn (Ver1.6).xla"
oldpath = "I:\Globcust\Corporate actions\System Liaison\VB\"
newpath = "H:\msoffice\XLStart"
UN = ucase(WshNetwork.UserName) 'Get GID
dim TimeOut
dim Response
TimeOut = 0
Response = 0
''''tell the user what's going on & give them chance to quit''''''''''
Response = msgbox ("You are about to run a macro which will update excel" & vbcrlf & _
"Excel will be closed (you will be asked to save any unsaved workbooks first" & vbcrlf & _
"Do you wish to continue?",36, "ATTENTION!")
if Response = 6 then
dim objProcessList
''''''shut down excel'''''''
on error resume next
Set gExcel = GetObject(,"Excel.Application")
gExcel.visible = true
gExcel.displayalerts = true
gExcel.activeworkbook.close
gExcel.application.quit
gExcel = ""
on error goto 0
do ''''''start a loop to wait for all instances of excel to close before continuing'''''
''''''find excel in task manager process list''''''
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Excel.exe'")
x = false
'''''if excel is open change x to true'''''''
for each objprocess in colprocesslist
x = true
'msgbox objprocess.name
next
''''''if x is false then no excel apps are open so we can carry on with the update''''''''''
if x = false then
exit do
end if
''''''ease the pressure on the old cpu whilst the user sorts his s#@t out'''''''
wscript.sleep 2000
'''prevent an infinite loop incase the user has gone home or cancelled the excel shutdown !!''''
Timeout = TimeOut + 2000
'msgbox "timeout = " & timeout
''''I give the user 2 minutes to comply, otherwise, terminate process'''''
if timeout => 180000 then
msgbox "macro update timed out, please close any open excel spreadsheets and try again"
wscript.quit
end if
loop
''''now excel has closed the startup folder can be updated / created'''''
'''create appropriate folders and copy xla''''''''
If Not (FSO.folderexists(newpath)) then 'creates XLStart if there isn't a folder
FSO.createfolder(newpath)
FSO.CopyFile oldpath & newfile, newpath & newfile, True 'copy new file
End if
If FSO.FileExists(newpath & "\" & oldfile) then 'deletes old version
FSO.DeleteFile(newpath & "\" & oldfile)
End if
FSO.CopyFile oldpath & newfile, newpath & "\" & newfile, True 'copy new file
Info = Msgbox ("Macro Update Complete!", 64, "Information")
msgbox "Excel Add-In was updated"
else
msgbox "No update performed"
end if