How to Bring a Microsoft Edge Window to the Foreground with VBA in Word
Question details
The user needs to activate and bring a Microsoft Edge browser window to the front using VBA within a Word add-in.

- Product
- Microsoft Word
- Device & OS
- Windows
- Scenario
- Developing an Office add-in that requires switching focus to an existing Microsoft Edge window.
- Observed behavior
- Attempting to use Shell, PowerShell, and Windows API functions (like SetForegroundWindow) is complicated by quotation mark syntax and Windows security restrictions preventing background apps from stealing focus.
Ensure you have the Developer tab enabled in your Office application and save your work, as incorrect Windows API calls can crash the host application.
Use Windows API Functions in VBA
Directly call Windows API functions to locate the Microsoft Edge window and force it to the foreground.
To manipulate application windows outside of Office, you must declare Windows API functions at the top of a VBA module. The FindWindow function locates the Edge browser, and ShowWindow combined with SetForegroundWindow brings it to the front.
Note that modern Windows OS versions restrict background applications from stealing focus. You may need to ensure the Word add-in is currently the active window before attempting to shift focus to Edge.
Press ALT + F11 in Word to open the Visual Basic for Applications (VBA) editor.
Right-click on your project in the Project Explorer, select 'Insert', and then click 'Module'.
At the very top of the module, declare the necessary API functions: FindWindow, ShowWindow, and SetForegroundWindow using PtrSafe for 64-bit compatibility.
Create a subroutine that uses FindWindow with the Edge class name (usually 'Chrome_WidgetWin_1') or the exact window title to get the window handle (hWnd).
Use the retrieved hWnd to call ShowWindow(hWnd, 9) to restore the window, followed by SetForegroundWindow(hWnd) to bring it to the front.
Use PowerShell via VBA Shell Command
Use a PowerShell script executed from VBA to bypass some API complexities.
Seek Official VBA Developer Support
Consult Microsoft developer resources for advanced security restriction bypasses.
Develop and Run VBA Macros Seamlessly in WPS Office
WPS Office provides a highly compatible VBA environment, allowing you to write, edit, and execute macros—including complex Windows API calls—just as you would in Microsoft Office.
- 1. Enable the Developer Tools: Open WPS Writer, navigate to the top ribbon, and click on the 'Developer' tab.
- 2. Access the VBA Editor: Click the 'VBA Editor' icon (or press ALT + F11) to open the integrated development environment.
- 3. Create a New Module: Right-click your document project in the left pane, choose 'Insert', and select 'Module'.
- 4. Write and Execute API Code: Paste your SetForegroundWindow Windows API code and click 'Run' to execute the macro.

Frequently Asked Questions
Why does SetForegroundWindow flash the taskbar icon instead of bringing the window to the front?
Modern Windows operating systems implement focus-stealing prevention. If the process calling SetForegroundWindow (like Word) doesn't have the active focus or the rights to change it, Windows will just flash the target application's icon on the taskbar. To bypass this, developers sometimes use AttachThreadInput to share input states between threads.
How do I correctly pass quotation marks to a PowerShell script in a VBA Shell command?
In VBA, string literals are enclosed in double quotes. To include a literal double quote inside the string, you must type two double quotes ("") back-to-back, or concatenate the string using Chr(34). This ensures the command reaches PowerShell with the quotes intact.
Will Windows API calls like FindWindow work on macOS?
No, functions like FindWindow, ShowWindow, and SetForegroundWindow rely on the 'user32.dll' library, which is exclusive to the Windows operating system. If you are developing an add-in for Mac, you will need to use AppleScript via the MacScript or AppleScriptTask commands instead.
What is the PtrSafe keyword in VBA?
The PtrSafe keyword is required when declaring Windows API functions in 64-bit versions of Office. It tells the VBA compiler that the Declare statement is safe to run in a 64-bit environment, preventing compilation errors.




