Solved

Modify VBS/Batch Script to Terminate EXCEL.EXE Processes

Posted on 2014-04-23
6
2,816 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 27

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

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 27

Expert Comment

by:MacroShadow
ID: 40018402
Glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

920 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

15 Experts available now in Live!

Get 1:1 Help Now