Word VBA: Getting a Sentence of Cursor Position

The following Word VBA macro shows a message box with the sentence of cursor position.

Macro Example

Sub GetSentence()

  MsgBox Selection.Sentences(1).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.Sentences

  Selection.Sentences...

The Selection.Sentences property returns the Sentences collection object that represents all the sentences in the selection.

obj-Word-Selection-Sentences

Sentences.(1) (Sentences.Item(1))

  Selection.Sentences(1)..

The Sentences.Item method returns an individual Range object in a Sentences collection.

obj-Word-Sentences-Item

The .Item method is a default member of the Sentences collection object. We can see a little blue marble beside its icon in the Object Browser.
So, we can omit the .Item .

Sentences(1).Text (Range.Text)

  Selection.Sentences(1).Text

The Sentences(1).Text (Range.Text property) returns or sets the text in the specified range.

obj-Word-Range-Text

MsgBox function

The code: Selection.Sentences(1).Text is set to the parameter of a MsgBox function.

Sub GetSentence()

  MsgBox Selection.Sentences(1).Text

End Sub

So, a message box shows the sentence.

Properties

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

Apply To

  • Word 2013
  • Word 2010
  • Word 2007