0

I've created a single file.bat that execute correctly two different vbs files:

cscript "\\server_1\dir\file_M_1.vbs" "\\server_1\dir\muc1.xlsm"
cscript "\\server_2\dir\file_H_2.vbs" "\\server_2\dir\muc2.xlsm"

the code of the two files.vbs is the same, because it was created to do the same thing in two different servers.

This is the contents of file_M_1.vbs, that simply runs the macro called "copy_M":

Dim args, objExcel
Set args = WScript.Arguments
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open args(0)
objExcel.Visible = False
objExcel.Run "copy_M"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close(0)
objExcel.Quit

Instead this is the code of file_H_2.vbs, that runs macro called "copy_H":

Dim args, objExcel
Set args = WScript.Arguments
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open args(0)
objExcel.Visible = False
objExcel.Run "copy_H"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close(0)
objExcel.Quit

I' d like to know if there' s the possibility to collect all three pieces of code in only one file.bat or file.exe (to be even scheduled to run in a specific time with the task scheduler of windows7.)

user692942
  • 16,398
  • 7
  • 76
  • 175
il_betto
  • 21
  • 1
  • 4
  • 6
    Is it possible? Sure. Will we write the code for you? Not very likely. Modify the script to loop over `WScript.Arguments` and run the macros depending on which file you opened. Run the VBScript with an argument list consisting of both workbooks. – Ansgar Wiechers Jan 25 '17 at 09:30

1 Answers1

1

I' ve solved the problem in this way. With the help of others we have created only one file.bat:

cscript "\\server1\dir\file.vbs"

it runs the file.vbs:

Dim objExcel
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.open("\\server1\dir\muc1.xlsm")
objExcel.Visible = False
objExcel.Run "copy_M"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Workbooks.open("\\server2\dir\muc2.xlsm")
objExcel.Visible = False
objExcel.Run "copy_H"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Quit

it runs perfectly. I 've put in the task scheduler of windows7 the file.bat and all it' s ok.

I don' t know if it' s possible to create only one .exe (not a .bat that runs .vbs)

il_betto
  • 21
  • 1
  • 4
  • you can build a [hybrid file](http://stackoverflow.com/questions/38621603/hybrid-batch-vbs-autorun-as-administrator) – Stephan Jan 26 '17 at 10:00
  • I *strongly* recommend against writing Frankenscript like that. Maintaining and troubleshooting these is a major pain in the rear. – Ansgar Wiechers Jan 26 '17 at 21:05
  • @il_betto You may want to add `objExcel.DisplayAlerts = False` since you're running the script as a scheduled task. – Ansgar Wiechers Jan 26 '17 at 21:07