Focus problem calling Office 2007 on Vista...

Posted by Admin on 13-Jun-2007 02:09

Hello,

I have a small sample program that opens an Excel sheet:

DEFINE VARIABLE chExcel AS COM-HANDLE.

CREATE "Excel.Application" chExcel.

chExcel:Workbooks:open("c:\map1.xls").

chExcel:Visible = TRUE.

RELEASE OBJECT chExcel.

On Windows XP the Excel window opens above the Progress window and has focus.

On Windows Vista Excel opens underneath the Progress window and the Progress window keeps its focus.

This seems to be standard behaviour on Windows Vista (try starting another program from the quicklaunch bar while typing in Word, Word will keep its focus), but how can this be overcome?

I’ve tried chExcel:windows(1):activate but that doesn’t seem to do anything at all.

Thanks in advance for any help.

Lieven

All Replies

Posted by Admin on 15-Jun-2007 04:35

In case someone's interested, I've managed to work around the problem by forcing the window to the foreground using Windows API calls.

The functions needed are:

PROCEDURE BringWindowToTop EXTERNAL "user32.dll":

DEFINE INPUT PARAMETER hwndExcel AS LONG.

DEFINE RETURN PARAMETER lRet AS LONG.

END.

PROCEDURE FindWindowA EXTERNAL "user32.dll":

DEFINE INPUT PARAMETER lpClassName AS CHARACTER.

DEFINE INPUT PARAMETER lpWindowName AS CHARACTER.

DEFINE RETURN PARAMETER lRet AS LONG.

END.

From Excel 2002 onward the Excel com-handle has the "hwnd" property that can be used directly in the BringWindowToTop function. Unfortunately older versions + all other Office programs (Word, Outlook,...) don't have that property.

So first you have to get a HWND to the instance of Word/Outlook/... that you want to bring to the front.

To be sure you get the right Window, you can set it's caption (which is used as second parameter in FindWindowA) to a unique value.

Following code illustrates this for Word:

DEFINE VARIABLE chWordAppl AS COM-HANDLE NO-UNDO.

DEFINE VARIABLE cCaption AS CHARACTER NO-UNDO.

DEFINE VARIABLE iRet AS INTEGER NO-UNDO.

CREATE "Word.Application":U chWordAppl NO-ERROR.

cCaption = chWordAppl:Caption.

chWordAppl:Caption = "Find me please":U. /* temporary caption */

RUN FindWindowA("OpusApp":U, chWordAppl:Caption, OUTPUT hwndWord).

chWordAppl:Caption = cCaption.

RUN BringWindowToTop(chWordAppl:HWND, OUTPUT iRet).

The names of the windows classes names of Office can be found here:

http://users.skynet.be/am044448/Programmeren/VBA/vba_class_names.htm

Excel = "XLMAIN"

Word = "OpusApp"

Greets

Lieven

This thread is closed