internet-explorervbscriptvisibleforeground

VBScript, Open IE, sometimes visible=true brings it foreground, sometimes not


I am trying to open an Internet Explorer using

Set ie = createobject("InternetExplorer.Application")
ie.Navigate "www.google.com"
ie. Application.visible = true

Strangely, on some devices the new IE window will open in foreground(most front, become the active window) whereas others open in semi-foreground(usually second one, turns active when minimizes current active window).

Wondering if anyone knows what causes the differences? Any help is appreciated, thanks in advance!


Solution

  • From Help for SetForegroundWindow in Windows API.

    Remarks

    The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

    •The process is the foreground process.

    •The process was started by the foreground process.

    •The process received the last input event.

    •There is no foreground process.

    •The process is being debugged.

    •The foreground process is not a Modern Application or the Start Screen.

    •The foreground is not locked (see LockSetForegroundWindow).

    •The foreground lock time-out has expired (see
    SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).

    •No menus are active.

    An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user

    So what this means, if a foreground program starts a program the new program has two seconds to create or activate a window or can lose that right.