Malay
Malay

Reputation: 55

Opening EXCEL using batch file when multiple office versions are installed

Problem: Need a batch file that takes 2 args-->office version and operating system bits(32 or 64) and open the desired excel file.

something like:-

I call this file as Test 2010 64. This should allow me to open 2010 excel on a 64 bit machine.

Test.bat

set version=%1%
set bit=%2%

if "%version%" == "2010" if "%bit%" == "64" (cd "C:\Program Files\Microsoft Office\Office14")

if "%version%" == "2010" if "%bit%" == "32" (cd "C:\Program Files(x86)\Microsoft Office\Office14")

if "%version%" == "2013" if "%bit%" == "64" (cd "C:\Program Files\Microsoft Office\Office15")

if "%version%" == "2013" if "%bit%" == "32" (cd "C:\Program Files(x86)\Microsoft Office\Office15")

start excel.exe

The program is not working.Can someone please guide. I am using 64 bit WIN7 machine

Upvotes: 1

Views: 689

Answers (1)

Mofi
Mofi

Reputation: 49097

I suggest to use

set version=%~1
set bit=%~2

if "%version%" == "2010" if "%bit%" == "64" (
   cd /D "%ProgramFiles%\Microsoft Office\Office14"
   goto StartExcel
)
if "%version%" == "2010" if "%bit%" == "32" (
   cd /D "%ProgramFiles(x86)%\Microsoft Office\Office14"
   goto StartExcel
)
if "%version%" == "2013" if "%bit%" == "64" (
   cd /D "%ProgramFiles%\Microsoft Office\Office15"
   goto StartExcel
)
if "%version%" == "2013" if "%bit%" == "32" (
   cd /D "%ProgramFiles(x86)%\Microsoft Office\Office15"
   goto StartExcel
)
goto :EOF

:StartExcel
start excel.exe

The main mistake in your code was the percentage sign after %1 and %2 referencing already argument 1 and 2.

%~1 and %~2 is used in code above to remove double quotes if somebody calls the batch file with test.bat "2010" "64" although double quotes are not necessary for the 2 parameters.

Additionally %ProgramFiles% and %ProgramFiles(x86)% are used which lets the batch code work also on non English Windows XP x86 and x64 and on computers with standard program files directory not being on drive C:.

Switch /D is additionally used in case of program files directory is not on same drive as current working directory on execution of the batch file.

And last the batch file exits in case of no match, for example if the user of the batch file made a typing mistake on entering the 2 arguments.

Upvotes: 1

Related Questions