VBA & Macro Problems

How to Use VBA SendKeys to Select and Delete Notepad Text

Bushra ParveenBushra Parveen Nov 3, 2026 868 views

Question details

The user needs to automate opening a text file in Notepad, selecting all the existing text, and deleting it using a VBA macro.

How to Use VBA SendKeys to Select and Delete Notepad Text
Product
WPS Spreadsheet
Device & OS
not provided
Scenario
Automating text manipulation and clearing contents in an external application (Notepad) via VBA scripts.
Observed behavior
The user aims to use the Shell command alongside AppActivate and SendKeys to reliably send "Ctrl+A" and "Delete" commands to the active Notepad window.
Before you start

Ensure that macros are enabled in your spreadsheet application settings and that you have the correct file path to your target text file before running the VBA code.

Solution 1Recommended

Use a VBA Subroutine with Shell, AppActivate, and SendKeys

Create a macro that opens a text file using the Shell function, brings the Notepad window to the front, and simulates keyboard inputs to clear its contents.

The SendKeys function in VBA allows you to simulate keystrokes directly into the active application window. By pairing it with the Shell function, you can launch Notepad, activate it, and sequentially send 'Ctrl+A' followed by the 'Delete' key.

1
Open the VBA Editor

Press 'Alt + F11' in your spreadsheet application to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the Project Explorer pane, right-click anywhere, hover over 'Insert', and select 'Module'.

3
Paste the Macro Code

Copy and paste the following code into the module window: Sub ShellOpenNotepad() Dim returnValue As Variant Dim strPathFile As String strPathFile = Environ("UserProfile") & "\Documents\Notepad\Shell test.txt" returnValue = Shell("Notepad.exe " & strPathFile, vbNormalFocus) AppActivate returnValue SendKeys "^a" 'Select all text in the Notepad file SendKeys "{DEL}" 'Delete the selected text End Sub

4
Modify the File Path

Update the 'strPathFile' variable in the code to point to the exact location of your target Notepad text file.

5
Run the Macro

Press 'F5' or click the 'Run' button on the toolbar to execute the subroutine. Notepad will open, and the text will be automatically selected and deleted.

Use a VBA Subroutine with Shell, AppActivate, and SendKeys
Timing and Focus Issues: If Notepad does not register the keystrokes properly, consider adding a slight delay (e.g., Application.Wait) before executing the SendKeys commands to ensure the window is fully active and ready to receive input.
Automate with WPS Macros

Write and Run VBA Macros Easily in WPS Spreadsheet

WPS Spreadsheet fully supports VBA (Visual Basic for Applications), allowing you to write, test, and execute macros to automate your repetitive tasks smoothly.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open a new or existing spreadsheet document.
  2. 2. Access Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click on 'VBA Editor' to launch the macro environment.
  3. 3. Write Your Macro: Insert a new module and paste your SendKeys VBA code to instantly automate external apps like Notepad.
Fully compatible with Microsoft Excel VBA macros and scripts.Built-in robust VBA editor for creating complex automation flows.Free and lightweight office suite for everyday document management.
microsoft office alternative - wps office

Frequently Asked Questions

Why is SendKeys typing into my spreadsheet instead of Notepad?

This happens when the Notepad window loses focus before the SendKeys command executes. Using the AppActivate function with the process ID returned by Shell ensures the target window is active. Adding a brief pause using Application.Wait can also help establish focus.

What is the SendKeys code for the Delete key in VBA?

In VBA, you represent the Delete key as {DEL} or {DELETE} inside the SendKeys string. For example, using SendKeys "{DEL}" instructs the macro to press the Delete key on the keyboard.

Can I use VBA SendKeys to type new text after deleting the old content?

Yes. Immediately after your SendKeys "{DEL}" command, you can add another line like SendKeys "Your new text here" to automatically type fresh content into the active Notepad document.