Search code examples
excelbatch-filecmdrunas

Runas a certain xls file


I need to write a batch script which opens a certain xls file on behalf of another user account. runas separately works OK:

runas /profile /user:username "C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE"

excel connector is also OK:

"C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE /r C:\fol der\file.xls"

but together it's not:

runas /profile /user:username "C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE /r C:\fol der\file.xls"

It can't handle the path of the xls file if there is space anywhere.


Solution

  • As per RUNAS - Execute a program under a different user account: RUNAS used backslash \ as an escape character (not the standard ^ used by other CMD commands). Moreover, runas /? gives next example literally:

    runas /env /user:[email protected] "notepad \"my file.txt\""
    

    Hence, your line should be as follows:

    runas /profile /user:username "\"C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE\" /r \"C:\fol der\file.xls\""