Solved

Modify VBS/Batch Script to Terminate EXCEL.EXE Processes

Posted on 2014-04-23
6
2,661 Views
Last Modified: 2014-04-23
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
Comment
Question by:fireguy1125
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:pablito70
ID: 40018333
Try to add

objWorkbook.Close False

before closing Application.

HTH
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 40018334
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
 
LVL 1

Author Comment

by:fireguy1125
ID: 40018386
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40018392
Yes, after removing the first line "Option Explicit".
0
 
LVL 1

Author Closing Comment

by:fireguy1125
ID: 40018401
Thanks, works perfect!
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 40018402
Glad to help.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now