troubleshooting Question

VBS automatically reopen Excel file

Avatar of Lukáš Stacke
Lukáš Stacke asked on
Microsoft OfficeMicrosoft ExcelVB Script
5 Comments1 Solution165 ViewsLast Modified:
Hello Guys,
I have a script which opens all excel files in folder and run a macro in all of them. Unfortunately it keeps asking me that the excel file is already opened and if I would like to reopen it. I wanted to kill the process, but it doesn't really work as it should. Even when I manually kill the process and run the script it is asking me again, if i would like to reopen already open file. The problem with opening of excel occurs in  Sub ProcessExcelFile(sFileName).
Could you please advise what should be changed in bellow code? Thank you.

here is the code:

Const sRootFolder = "C:\test\"

Dim oFSO, oFile 
Dim xlApp, xlBook, objWorkbook 

 oShell.Run "taskkill /f /im EXCEL.EXE", , True

Sub Start()
     ProcessFilesInFolder sRootFolder
 End Sub

Sub ProcessFilesInFolder(sFolder)
     ' Process the files in this folder
     For Each oFile In oFSO.GetFolder(sFolder).Files
         If IsExcelFile(oFile) Then ProcessExcelFile oFile.Path
 End Sub

Sub Initialize()
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     Set xlApp = CreateObject("Excel.Application")   
 End Sub

Sub Finish()
     Set xlBook = Nothing 
     Set xlApp = Nothing    
     Set oFSO = Nothing
 End Sub

Function IsExcelFile(oFile)
     IsExcelFile = (InStr(1, oFSO.GetExtensionName(oFile), "xls", vbTextCompare) > 0) And (Left(oFile.Name, 1) <> "~")
 End Function

Sub ProcessExcelFile(sFileName)   
     Set xlBook = xlApp.Workbooks.Open(sFileName, 0, True) 
     Set objWorkbook = xlApp.Workbooks.Open(sFileName)     
     xlApp.Run "import_test"
 End Sub 
 Sub Save()

oShell.Run "taskkill /f /im EXCEL.EXE", , True
  WScript.Echo "Finished."
 End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros