user3903489
user3903489

Reputation: 61

Excel VBA to Word Unselect a Selected text

I'm writing an Excel VBA code that transfers data to a Word document. I'm using the command Application.Selection.Find and Application.Selection.InsertAfter in order to fill in the blanks in the Word document.

The problem is that after I select one item I can't select another one.

I would like to be able to unselect an item so that I could freely select another using the same command. How?

Upvotes: 6

Views: 13272

Answers (3)

MustardMan
MustardMan

Reputation: 111

You have more than one option depending on where you want the cursor after the operation. Option 1 is probably the most common presuming you are done working on the current selection and want to move forward, but in some cases you may want to do something just before that selection, but be careful to not enter into an infinite loop.

Option 1

This unselects the selection and puts the cursor after the current selection.

Selection.EndOf

Option 2

This unselects the selection and puts the cursor before the current selection. Note: with the cursor placement before the current selection it is possible to enter an infinite loop.

Selection.Collapse

Option 3???

Microsoft says that this is an option, but I am working in Word 2013 and it is not an option for me. Maybe it works in something more recent than 2013? Ref https://learn.microsoft.com/en-us/office/vba/api/publisher.selection.unselect

ActiveDocument.Selection.Unselect

Upvotes: 2

Jek Denys
Jek Denys

Reputation: 115

Alternatively can use this. This will send the cursor to the end of the Word document.
Selection.EndKey Unit:=wdStory

[SOURCE: https://software-solutions-online.com/word-vba-move-cursor-to-end-of-document/]

Upvotes: 1

Can use Application.Selection.EndOf.

Upvotes: 7

Related Questions