Restore Text or Shape Selection After a Word VBA Macro Runs
Question details
Restore the original text or floating shape selection after a Word VBA macro runs.

- Product
- Microsoft Word 2013 VBA
- Device & OS
- not provided
- Scenario
- A macro creates a text box and needs to return the user to the text or shape that was selected beforehand.
- Observed behavior
- Text selections restore correctly, but restoring a floating shape through its text range can leave an abnormal selection marker. The shape cannot be formatted until it is clicked again.
Open the document in Microsoft Word and use Alt+F11 to open the VBA editor. Check whether the macro changes the selection; if it can work directly with Range and Shape objects, restoring the selection may not be necessary.
Save and restore the selection according to its type
Use this approach when a macro changes the selection and must restore either selected text or a floating shape.
A text Range and a floating ShapeRange represent different kinds of selection. Save the ShapeRange before the macro changes the selection rather than relying on the saved text range to identify the shape.
In Word, press Alt+F11 and open the module containing your macro. Select a text range or floating shape in the document before running the example.
Adapt this example in your module. It creates and selects a new text box, then restores the original selection: Sub RestoreOriginalSelection() Dim savedDoc As Document Dim savedRange As Range Dim savedShapes As ShapeRange Dim wasShape As Boolean Dim newBox As Shape Set savedDoc = ActiveDocument Set savedRange = Selection.Range.Duplicate wasShape = (Selection.Type = wdSelectionShape) If wasShape Then Set savedShapes = Selection.ShapeRange Set newBox = savedDoc.Shapes.AddTextbox( _ Orientation:=msoTextOrientationHorizontal, _ Left:=72, Top:=72, Width:=144, Height:=36) newBox.Select savedDoc.Activate If wasShape Then savedShapes.Select Else savedRange.Select End If End Sub
Run the macro with text selected, then select a floating shape and run it again. Confirm that the original text or shape is selected and that the shape's formatting controls are available.

Edit Word Documents with WPS Writer
WPS Writer is a free, lightweight alternative with a familiar interface for editing documents and shapes. Open compatible Word files to make migration easier; it does not fix Microsoft Word's VBA selection behavior.

Frequently Asked Questions
Why doesn't selecting the saved range restore my shape?
A floating shape selection is not the same as a text range selection. Save Selection.ShapeRange before the macro changes the selection, then call its Select method to restore the shape.
How can I tell whether a floating shape is selected?
In Word VBA, test whether Selection.Type equals wdSelectionShape before saving Selection.ShapeRange.
Can I avoid changing the selection in my macro?
Often, yes. Work directly with Range and Shape objects instead of calling Select when creating or modifying document content. The original selection may then remain in place.
Does this method also restore inline pictures?
The example is intended for text and floating shapes. Inline objects use a different selection type, so test and handle them separately in your macro.
Will WPS Writer fix a selection problem in Word VBA?
No. The VBA selection issue belongs to Microsoft Word. WPS Writer is an alternative for editing compatible documents, but VBA behavior and support may differ.




