Word VBA: Getting the Style Name of Current Cursor Position

The following Word VBA macro displays the Style name of current cursor position in a message box.

Macro Example

Sub GetStyleName()

  MsgBox Selection.Style.NameLocal

End Sub

Description

Selection property

  Selection...

The Application.Selection property returns the Selection object that represents the current selection in a window or pane of Microsoft Word.

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.Style property

  Selection.Style...

The Selection.Style property returns or sets the Style object for the Selection.

obj-Word-Selection-Style

Style.NameLocal property

  Selection.Style.NameLocal

The Style.NameLocal property returns the name of a style.

MsgBox function

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

  MsgBox Selection.Style.NameLocal

So, a message box shows the Style name of current cursor position.

Sub GetStyleName()

  MsgBox Selection.Style.NameLocal

End Sub

Properties

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

Apply To

  • Word 2013
  • Word 2010
  • Word 2007