VBA & Macro Problems

How to Send Excel VBA SendKeys to a Web Browser

Maira MehtabMaira Mehtab Nov 4, 2026 869 views

Question details

The user needs to send automated keystrokes to a webpage using Excel VBA, but the SendKeys commands are executing in the wrong window.

How to Send Excel VBA SendKeys to a Web Browser
Product
Excel
Device & OS
Windows
Scenario
Automating browser interactions by opening a webpage via FollowHyperlink and passing keystrokes.
Observed behavior
SendKeys commands are being sent to the VBA editor rather than the web browser window.
Before you start

Ensure you have the exact window title of your target web browser, and save your Excel workbook before running SendKeys macros to prevent unintended data loss.

Solution 1Recommended

Use Windows API to Bring the Web Browser to Foreground

Declare Windows API functions in your VBA module to locate the browser window and force it into the foreground before executing SendKeys.

Although VBA is not C++, you can seamlessly call Windows API functions directly within the Excel VBA editor. The FindWindow and SetForegroundWindow functions allow you to grab the browser's window handle and bring it to the front.

1
Open the VBA Editor

Press Alt + F11 in Excel to open the VBA Editor, then click Insert > Module to create a new module.

2
Declare the Windows API Functions

At the very top of your module, paste the declarations: 'Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr' and 'Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long'.

3
Add a Delay After Opening the Browser

In your macro, after calling 'ActiveWorkbook.FollowHyperlink', add a short delay using 'Application.Wait (Now + TimeValue("0:00:03"))' to ensure the browser has fully loaded.

4
Set Focus and Send Keystrokes

Use 'Dim hwnd As LongPtr', then 'hwnd = FindWindow(vbNullString, "Exact Browser Window Title")'. Call 'SetForegroundWindow(hwnd)' right before your 'Application.SendKeys' commands.

Use Windows API to Bring the Web Browser to Foreground
SendKeys Reliability: SendKeys can be unreliable for web automation. If a system notification pops up or focus shifts during execution, keystrokes may be sent to the wrong application.
Free Microsoft Office alternative

Looking for a Lightweight Alternative for Your Spreadsheets?

While VBA SendKeys issues often stem from system-level window focus limitations in Windows and Microsoft Office, you might be looking for a smoother, more lightweight office suite. WPS Office provides excellent compatibility with Microsoft Office formats in a highly efficient package.

  1. 1. Download WPS Office: Visit the official WPS Office website and click the Free Download button.
  2. 2. Install the Software: Run the downloaded installer and follow the simple on-screen instructions.
  3. 3. Open Your Files: Launch WPS Spreadsheets and open your existing Excel files seamlessly.
Highly compatible with Microsoft Excel (.xlsx, .xls, .csv) formatsLightweight installation that uses fewer system resourcesFree and easy to use with a familiar tabbed interfaceSeamless migration for all your daily spreadsheet tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why are my Excel VBA SendKeys going to the VBA editor?

This happens because the FollowHyperlink method opens the browser in the background, while the VBA editor retains the active system focus. The SendKeys function sends keystrokes to whichever window is currently active, which in this case remains the editor.

Do I need to know C++ to use Windows API in Excel VBA?

No, you do not need to know C++. You only need to copy the correct API function declarations (like FindWindow and SetForegroundWindow) into the top of your VBA module and call them just as you would call standard built-in VBA functions.

Are there better alternatives to using SendKeys for web automation?

Yes. Using SendKeys is generally discouraged for web automation because it strictly relies on window focus and timing. Dedicated tools like Selenium for VBA, or utilizing direct API requests (like XMLHTTP), provide much more reliable results without hijacking your keyboard.