How to Use VBA SendKeys to Select and Delete Notepad Text
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.

- 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.
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.
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.
Press 'Alt + F11' in your spreadsheet application to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane, right-click anywhere, hover over 'Insert', and select 'Module'.
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
Update the 'strPathFile' variable in the code to point to the exact location of your target Notepad text file.
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.

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. Open WPS Spreadsheet: Launch WPS Office and open a new or existing spreadsheet document.
- 2. Access Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click on 'VBA Editor' to launch the macro environment.
- 3. Write Your Macro: Insert a new module and paste your SendKeys VBA code to instantly automate external apps like Notepad.

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.




