sjngm
sjngm

Reputation: 12861

Set focus to an application with batch

I'm using WinXP and I have a batch script where I determine the filename of an Excel sheet and then launch it by simply launching that Excel sheet.

The problem is that Excel is running at that time. So when the batch file launches the Excel sheet, Excel is sent to front and the Excel sheet opens. However, the focus still is on the DOS-box where the batch file is running.

How do I set the focus to the Excel sheet in this situation?

Upvotes: 2

Views: 6773

Answers (2)

Yaron
Yaron

Reputation: 173

If you want to focus another program you can do this.

call :focus WindowTitle
exit /b

:focus
setlocal EnableDelayedExpansion 

    if ["%~1"] equ [""] (
        echo Please give the window's title.
        exit /b
    )

    set pr=%~1
    set pr=!pr:"=!

    echo CreateObject("wscript.shell").appactivate "!pr!" > "%tmp%\focus.vbs"
    call "%tmp%\focus.vbs"
    del "%tmp%\focus.vbs"

goto :eof 
endlocal 

I am using vbscript to focus the application. You need to pass the window's title, not the window's name (whatever.bat). To make sure you get the right

Upvotes: 0

Anthony Miller
Anthony Miller

Reputation: 15920

Try passing the file directly to Excel:

excel.exe "C:\Path\To\file.xlsx"

Can't test as we use open office.

EDIT:

START /B (excel.exe "C:\Path\To\file.xlsx")

Upvotes: 1

Related Questions