ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Programmatically Restart Excel (https://www.excelbanter.com/excel-programming/401378-programmatically-restart-excel.html)

Drew Lettington

Programmatically Restart Excel
 
When users change certain configuration options in my Excel add-in I need
them to restart Excel to have them applied - for example, the user interface
language. I can either information the user that they need to restart or
close Excel and require them to restart it. What I want to do is
programmatically restart Excel like Excel does itself when you get the
message Excel has encountered a problem and needs to restart. Is there a way
I can have my add-in shut down Excel so it will automatically restart?

- Drew

RB Smissaert

Programmatically Restart Excel
 
You could create a Windows task, which will start Excel or you could pass
control to a VB6 .exe file. Not sure there is a simpler way as if Excel is
closed it can't start Excel, unless Excel starts a second instance of Excel
and
then closes itself with Quit. Just some quick thoughts and I am sure
somebody
will come up with a better answer.

RBS


"Drew Lettington" wrote in
message ...
When users change certain configuration options in my Excel add-in I need
them to restart Excel to have them applied - for example, the user
interface
language. I can either information the user that they need to restart or
close Excel and require them to restart it. What I want to do is
programmatically restart Excel like Excel does itself when you get the
message Excel has encountered a problem and needs to restart. Is there a
way
I can have my add-in shut down Excel so it will automatically restart?

- Drew



Steve Yandl

Programmatically Restart Excel
 
Drew,

Below is one option. The subroutine writes a vbs file to the temp folder
then it launches the vbs file immediately before saving the workbook and
closing Excel. The first line in the vbs file causes a 10 second pause to
allow Excel time to shut down, then it launches Excel and opens the workbook
from whence it came and finally deletes itself from the temp folder.

______________________________________

Sub RestartMe()

strXLfile = ThisWorkbook.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)

ThisWorkbook.Save
Application.Quit

Set fso = Nothing

End Sub

______________________________________

Steve Yandl




"Drew Lettington" wrote in
message ...
When users change certain configuration options in my Excel add-in I need
them to restart Excel to have them applied - for example, the user
interface
language. I can either information the user that they need to restart or
close Excel and require them to restart it. What I want to do is
programmatically restart Excel like Excel does itself when you get the
message Excel has encountered a problem and needs to restart. Is there a
way
I can have my add-in shut down Excel so it will automatically restart?

- Drew




Steve Yandl

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



Drew Lettington

Programmatically Restart Excel
 
Thanks RB and Steve. I'm going to try something similar to what you've
suggested. I think I'll write a small application to start Excel. The
add-in will call the application prior to closing Excel, passing a parameter
with the process ID of the currently running Excel process. The application
will check to see then the process is no longer available and then will start
Excel.

- Drew

"Steve Yandl" wrote:

Drew,

Below is one option. The subroutine writes a vbs file to the temp folder
then it launches the vbs file immediately before saving the workbook and
closing Excel. The first line in the vbs file causes a 10 second pause to
allow Excel time to shut down, then it launches Excel and opens the workbook
from whence it came and finally deletes itself from the temp folder.

______________________________________

Sub RestartMe()

strXLfile = ThisWorkbook.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)

ThisWorkbook.Save
Application.Quit

Set fso = Nothing

End Sub

______________________________________

Steve Yandl




"Drew Lettington" wrote in
message ...
When users change certain configuration options in my Excel add-in I need
them to restart Excel to have them applied - for example, the user
interface
language. I can either information the user that they need to restart or
close Excel and require them to restart it. What I want to do is
programmatically restart Excel like Excel does itself when you get the
message Excel has encountered a problem and needs to restart. Is there a
way
I can have my add-in shut down Excel so it will automatically restart?

- Drew





Steve Yandl

Programmatically Restart Excel
 
Drew,

I thought about setting the vbs file to monitor for the Excel process being
shut down using WMI but wasn't certain that the systems would all have WMI
installed (Win 98 would not). Also, when you're having the
Scripting.FileSystemObject create the vbs file by writing line by line, it
gets to be cumbersome when you have WMI statements with lots of punctuation
marks. It's certainly more straightforward to have your application already
set to go and passing it an appropriate argument but that means you need to
have the helper application in place before your users run your routine.

Steve



"Drew Lettington" wrote in
message ...
Thanks RB and Steve. I'm going to try something similar to what you've
suggested. I think I'll write a small application to start Excel. The
add-in will call the application prior to closing Excel, passing a
parameter
with the process ID of the currently running Excel process. The
application
will check to see then the process is no longer available and then will
start
Excel.

- Drew

"Steve Yandl" wrote:

Drew,

Below is one option. The subroutine writes a vbs file to the temp folder
then it launches the vbs file immediately before saving the workbook and
closing Excel. The first line in the vbs file causes a 10 second pause
to
allow Excel time to shut down, then it launches Excel and opens the
workbook
from whence it came and finally deletes itself from the temp folder.

______________________________________

Sub RestartMe()

strXLfile = ThisWorkbook.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)

ThisWorkbook.Save
Application.Quit

Set fso = Nothing

End Sub

______________________________________

Steve Yandl




"Drew Lettington" wrote in
message ...
When users change certain configuration options in my Excel add-in I
need
them to restart Excel to have them applied - for example, the user
interface
language. I can either information the user that they need to restart
or
close Excel and require them to restart it. What I want to do is
programmatically restart Excel like Excel does itself when you get the
message Excel has encountered a problem and needs to restart. Is there
a
way
I can have my add-in shut down Excel so it will automatically restart?

- Drew








All times are GMT +1. The time now is 10:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com