Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4128
  • Last Modified:

Modify VBS/Batch Script to Terminate EXCEL.EXE Processes

I'm using a combination of the below scripts to rename an xlsx extension file to an xls extension file.  I find that sometimes excel remains running and prevents the script from executing.  I would like to include a line in either file to terminate all running instances of excel.exe so the script can process successfully. Presently, I'm going through the task manager to end the process manually.

Batch file:
C:\Scripts\xlsx2xls.vbs "C:\report.xlsx" "C:\report.xls"

Open in new window


VBS file:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(Wscript.Arguments(0))
objExcel.Application.Visible = False
objExcel.Application.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs Wscript.Arguments(1), 56
objExcel.ActiveWorkbook.Close
objExcel.Application.DisplayAlerts = True
objExcel.Application.Quit
WScript.Quit

Open in new window

0
fireguy1125
Asked:
fireguy1125
  • 3
  • 2
1 Solution
 
pablito70Commented:
Try to add

objWorkbook.Close False

before closing Application.

HTH
0
 
MacroShadowCommented:
This script will terminate any orphan instances of Excel that are running:
Option Explicit

Dim objWMIService, objProcess, colProcess

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & "'EXCEL.EXE'")

For Each objProcess in colProcess
    objProcess.Terminate()
Next

Open in new window

0
 
fireguy1125Author Commented:
Thanks, can I put those 10 lines of code just as they are, preceding what I have in my vbs file?

So should the following work?

Option Explicit
Dim objWMIService, objProcess, colProcess
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & "'EXCEL.EXE'")
For Each objProcess in colProcess
    objProcess.Terminate()
Next
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(Wscript.Arguments(0))
objExcel.Application.Visible = False
objExcel.Application.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs Wscript.Arguments(1), 56
objExcel.ActiveWorkbook.Close
objExcel.Application.DisplayAlerts = True
objExcel.Application.Quit
WScript.Quit

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MacroShadowCommented:
Yes, after removing the first line "Option Explicit".
0
 
fireguy1125Author Commented:
Thanks, works perfect!
0
 
MacroShadowCommented:
Glad to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now