9

I designed an excel spreadsheet that takes data from a server using an RTD feed and processes it. I want the excel file to open automatically during the computers startup. The way that I have decided to go about doing this is to write a batch script that opens the excel file and to then put that batch script in the computers startup folder.

The problem I am running into relates to the batch script. The RTD feed does not work if I use the default shortcut for excel. Instead I have to use a shortcut that has the following target line:

 "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" /a "CompanyExcelAddin.CompanyFunctions"

I am able to open the file using this command line

start `"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" "C:\...\filename.xlsm"`

but I am not able to open a file using the following bash command

start "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" /a "CompanyExcelAddin.CompanyFunctions" "C:\...\filename.xlsm"

If I open it using the first batch script the RTD feed doesn't work. If I try to run the second script the batch script doesn't run.

How do I write a batch script that takes command line arguments for the startup program?

alluppercase
  • 304
  • 1
  • 4
  • 12
  • Batch scripts and bash scripts are different animals, powershell even moreso. Please edit your 'question' by removing the non necessary tags and changing what is a broad/generic question to something more specific. As you currently have it, it looks like you are requesting a free solution using any method which is both rude and technically off topic. – Compo Aug 01 '17 at 16:34
  • Put an empty pair of quotes before any path components, like `start "" "C:\Program Files (x86)\...\EXCEL.EXE" /a ...`, because `start` often interprets the first quoted string as a window title... – aschipfl Aug 01 '17 at 17:47

2 Answers2

10
@echo off
set params=%*
start excel "MyWorkbook.xlsm" /e/%params%

Let's suppose you named it "MyBatch.bat", then you call it like this:

MyBatch.bat Hello/World1

Use space " " to separate parameters. Use slash "/" instead of space for parameters with spaces.

In case you do not like the string I believe you can also do this (in a *.bat file):
start excel "MyWorkbook.xlsm" /e/%param1%/%param2%/%param3%.....

In case you need to open several Excel instances:

@echo off
set params=%*
for %%C in (%params%) do (
    start excel "MyWorkbook.xlsm" /e/%%C
)
Sergio Muriel
  • 1,039
  • 9
  • 15
  • How can I open a protected sheet/workbook from this same batch script ? Is it just a matter of passing another parameter or a whole different story ? – Vicky Dev Dec 18 '21 at 22:07
  • [Excel VBA - Automatically Input Password](https://stackoverflow.com/questions/11326986/excel-vba-automatically-input-password) – Sergio Muriel Dec 20 '21 at 00:07
1

First, I'd recommend using a scheduled task over the startup folder (consistent behavior). Trigger on login, or however you need it, and execute powershell -ExecutionPolicy Bypass -NoProfile -File 'thing.ps1'. It looks like you may be passing the arguments out of order for the Excel exe.

$EXE = "${env:ProgramFiles(x86)}\Microsoft Office\root\Office16\EXCEL.EXE"
& $EXE 'C:\...\filename.xlsm' /a 'CompanyExcelAddin.CompanyFunctions'
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63