0

I am trying to write a batch file that will open two Excel documents and close them without losing data.

I need this because the first document is automatically downloaded from the Internet every day and contains raw data. The second document uses these data from the first document and arranges them in a more intelligible way. In order to update the second document, the first one needs to be open. The second document contains a VBA code that will save it on closing (without any prompt/notification per settings).

I am not a programmer, but with the help of Google I managed to write the following code:

@echo off
start excel "c:\data.csv"
timeout 5
start excel "c:\formatted_data.xlsm"
timeout 5
taskkill /F /IM excel.exe
exit

It all works fine apart from closing the document. It kills the process forcefully and the VBA code contained in c:\formatted_data.xlsm that's supposed to save the document on closing doesn't work then.

Is there any "gentler" way of closing the document using a batch file, that works in the same way as if I pressed the cross in the upper right corner, so that the updated document will be saved on closing?

Thank you in advance for your help.

Compo
  • 36,585
  • 5
  • 27
  • 39
Dunno123
  • 77
  • 5
  • 1
    Have you tried `TaskKill` without the `/F`orceful termination option, to see if that makes any difference? – Compo May 21 '19 at 01:01
  • 1
    I'm confused as to why you need the batch script at all. Excel can easily open the CSV file for writing, save itself, and close itself. – SomethingDark May 21 '19 at 02:05
  • Compo: Oh, that works! Thank you! – Dunno123 May 21 '19 at 09:29
  • SomethingDark: Because I only need to close the document when it updates itself. When anybody wants to open the document at a later time, they couldn't use it if it always closed itself automatically. – Dunno123 May 21 '19 at 09:31

0 Answers0