Word VBA: Getting Selected Text

The following Word VBA macro shows a message box with the selected text in a Microsoft Word document.

Macro Example

Sub GetSelectedText()

  MsgBox Selection.Text

End Sub

Description

[Application.]Selection

  Selection...

The Application.Selection property returns the Selection object that represents a selected range or the insertion point.

obj-Word-Application-Selection

The Selection property is a member of the Word.Global class, so we can omit the Application property.

obj-Word-Global-Selection

Selection.Text

  Selection.Text

The Selection.Text property returns or sets the text in the specified selection.

MsgBox function

The code: Selection.Text is set to the parameter of a MsgBox function.

Sub GetSelectedText()

  MsgBox Selection.Text

End Sub

So, a message box shows the selected text in a Microsoft Word document.

Properties

http://www.relief.jp/itnote/archives/word-vba-get-selection-text.php

Apply To

  • Word 2013
  • Word 2010
  • Word 2007