Fix Word VBA Macro to Find the Second Occurrence of Selected Text
Question details
The user needs a Word VBA macro to search for the next or previous instance of currently selected text, and a fix for the 'Ambiguous name detected' compile error.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Writing a VBA script to automate searching for the next or previous instance of a highlighted word without using SendKeys.
- Observed behavior
- The user encounters an 'Ambiguous name detected' compile error when trying to run the macro, caused by having duplicate procedure names in the VBA project.
Ensure that the Developer tab is enabled in your Word ribbon so you can access the Visual Basic Editor (VBA) by pressing Alt + F11.
Create Unique Macros for Next and Previous Occurrences
Use distinct subroutine names and specific collapse directions to find the next or previous occurrence of your selected text.
The 'Ambiguous name detected' error occurs when multiple subroutines (Macros) share the exact same name, such as having two 'Sub FindNextOccurrence()' blocks in your code. To fix this, you must rename one of them.
Additionally, you need to tell Word which direction to search. Use 'wdCollapseEnd' with 'Forward:=True' to find the next occurrence, and 'wdCollapseStart' with 'Forward:=False' to find the previous occurrence.
Press 'Alt + F11' on your keyboard to open the Microsoft Visual Basic for Applications window.
In the Project Explorer pane on the left, right-click 'Normal' or your document name, select 'Insert', and then click 'Module'.
Paste the following code to find the next occurrence: Sub FindNextOccurrence() Dim s As String s = Selection.Text Selection.Collapse Direction:=wdCollapseEnd Selection.Find.Execute FindText:=s, Forward:=True End Sub
Paste the following code below the first one, ensuring the name is unique: Sub FindPreviousOccurrence() Dim s As String s = Selection.Text Selection.Collapse Direction:=wdCollapseStart Selection.Find.Execute FindText:=s, Forward:=False End Sub
Close the VBA editor. Highlight a word in your document, go to the 'Developer' tab, click 'Macros', select either 'FindNextOccurrence' or 'FindPreviousOccurrence', and click 'Run'.

Automate Your Workflow with WPS Office Macros
WPS Office provides excellent support for VBA macros in its professional editions, allowing you to run scripts for text searches and document formatting just like you would in Microsoft Word.
- 1. Open WPS Writer: Launch WPS Office and open your document in the WPS Writer application.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon menu to access macro settings.
- 3. Open the VBA Editor: Click on 'Macros' or press 'Alt + F11' to open the built-in VBA editor.
- 4. Paste and Run Your Code: Insert a new module, paste your Find Text macro code, and run it directly within your WPS document.

Frequently Asked Questions
What causes the 'Ambiguous name detected' error in Word VBA?
This compile error occurs when two or more procedures (like 'Sub' or 'Function') in the same VBA module have the exact same name. Renaming one of the procedures to be unique will fix the error.
Why do I need to use Selection.Collapse in my macro?
Using Selection.Collapse deselects the currently highlighted text and moves the cursor to either the beginning (wdCollapseStart) or the end (wdCollapseEnd) of the selection. This sets the correct starting point so the macro searches for the next or previous instance rather than getting stuck on the currently selected word.
How do I save a Word document that contains VBA macros?
Standard Word documents (.docx) cannot save macro code. You must use 'Save As' and choose 'Word Macro-Enabled Document (*.docm)' to ensure your VBA scripts are preserved when you close the file.




