Falhuddin
Falhuddin

Reputation: 103

How to CLOSE and SAVE Excel file in endless running batch file only if the Excel file is open?

I have application that generate data into excel file and its automatically open the excel file after that. So, I want to create batch command to Close and Save the excel file only if the excel is open and this command need to be loop continuously. If the command close and save specific excel file will be great. Here my current command :

@echo off
:start
tasklist /fi "imagename eq EXCEL.EXE" |find ":" > nul
if errorlevel 1 taskkill /f /im "EXCEL.EXE"&exit
goto start

This command is just forcely close the excel file without saving and its not looping.

Upvotes: 0

Views: 2068

Answers (1)

Falhuddin
Falhuddin

Reputation: 103

To perform this task, 2 type of script are needed. Batch Script is to Check if either Application is running or not, if application is run then it will call VB Script else it will go to start.

Batch Command :

    :start
    tasklist /FI "IMAGENAME eq EXCEL.EXE" 2>NUL | find /I /N "EXCEL.EXE">NUL
    if "%ERRORLEVEL%"=="0" (goto Call) else (goto start)

    :Call
    TIMEOUT /T 5
    @call cscript "C:\Temp\Test.vbs"     '<--VBScript location
    goto start

VBScript Command :

Option Explicit

Dim objXL, xlnormal, xlsxformat

On Error Resume Next

Set objXl = GetObject(, "Excel.Application")
xlnormal = &HFFFFEFD1    '# use this for .xls
xlsxformat = &H33        '# use this for .xlsx

On Error Goto 0

On Error Resume Next
objXL.ActiveWorkbook.Save
On Error Goto 0

objXL.Quit
On Error Goto 0

Upvotes: 1

Related Questions