Programmatically Restart Excel
Drew,
Since you say you're running all of this from an add-in rather than the
active workbook, you should probably replace ThisWorkbook with
ActiveWorkbook in the sub.
Sub RestartMe()
strXLfile = ActiveWorkbook.FullName
Set fso = CreateObject("Scripting.FileSystemObject")
strRestarter = fso.GetSpecialFolder(2) & "\restartWkbk.vbs"
Set fileRestart = fso.CreateTextFile(strRestarter)
With fileRestart
.WriteLine "WScript.Sleep 10000"
.WriteLine "Set fso = CreateObject(" & Chr(34) _
& "Scripting.FileSystemObject" & Chr(34) & ")"
.WriteLine "Set objXL = CreateObject(" & Chr(34) _
& "Excel.Application" & Chr(34) & ")"
.WriteLine "objXL.Visible = True"
.WriteLine "objXL.Workbooks.Open(" & Chr(34) _
& strXLfile & Chr(34) & ")"
.WriteLine "fso.DeleteFile " & Chr(34) & strRestarter & Chr(34)
.Close
End With
strCmdRstrt = "WScript.exe " & strRestarter
Shell (strCmdRstrt)
ActiveWorkbook.Save
Application.Quit
Set fso = Nothing
End Sub
|