How to Find the Next Non-Space Character Using Word VBA
Question details
The user wants to detect the next character in a Word document that is not a space, while also properly distinguishing standard spaces from paragraph marks.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Writing a VBA macro to navigate text and identify specific non-space and non-paragraph characters.
- Observed behavior
- The macro needs to identify character codes, specifically distinguishing spaces (ASCII 32) and paragraph marks (ASCII 13 or vbCr) to isolate the target character.
Ensure you have the Developer tab enabled in your Word processor to access the VBA editor, and save your document before running custom macros to prevent unintended changes.
Use Character Codes to Detect Non-Space Characters
Collapse the current text selection, move forward one character at a time, and evaluate the ASCII character codes to skip spaces and paragraph marks.
In Word VBA, every character has an associated ASCII code. A standard space is represented by the code 32, while a paragraph mark is 13 (often represented as vbCr). By checking these values, your macro can easily skip them to find the next valid text character without needing to display a message to the user.
Press Alt + F11 to open the Visual Basic Editor and insert a new Module where you will write the macro.
Start your code by collapsing the current selection so it acts as a cursor. Use the command `Selection.Collapse Direction:=wdCollapseEnd`.
Create a loop that moves the selection forward one character at a time using `Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend`.
Within the loop, test the selected character's code using `Asc(Selection.Text)`. Set the loop to continue as long as the ASCII value is 32 (space) or 13 (paragraph mark).
Once the loop encounters a character with an ASCII value other than 32 or 13, the loop stops, leaving the selection on the first character that is neither a space nor a paragraph mark.

Write and Run VBA Macros Seamlessly in WPS Office
WPS Office provides robust support for VBA macros. You can execute character-detection scripts exactly as you would in Microsoft Word, ensuring high compatibility and smooth automation.
- 1. Enable Developer Tools: Open WPS Writer and navigate to the Developer tab on the top ribbon.
- 2. Access the VBA Editor: Click on 'Macros' or 'Visual Basic' to launch the integrated VBA Editor.
- 3. Execute Your Script: Paste your character detection code into a new module and click the Run button to test it on your document.

Frequently Asked Questions
How do I test for a paragraph mark in Word VBA?
You can test for a paragraph mark by comparing the character against the constant `vbCr` or by checking if its ASCII character code using `Asc(character)` equals 13.
What is the ASCII value for a standard space?
The ASCII value for a standard space is 32. In a VBA macro, you can check this condition using the expression `Asc(Selection.Text) = 32`.
How can I move the selection without displaying a prompt?
Use the `Selection.Move` or `Selection.MoveRight` methods within your VBA code to quietly shift the cursor position. As long as you do not include `MsgBox` commands in your loop, the macro will run silently.
Does WPS Office support VBA character code checks?
Yes, WPS Office fully supports standard VBA syntax, including the `Asc()` function required to evaluate character codes like spaces and paragraph marks.




