Word Macro: Switching an Anchor Lock

This Word VBA macro switches on and off the anchor lock.

Macro Example

Sub ToggleAnchorLock()

  With Selection
    If .Type = wdSelectionShape Then
      .ShapeRange.LockAnchor = wdToggle
    End If
  End With

End Sub

Description

Starts the With Statement

Sub...

  With Selection

The 3rd line starts the With…End With statement.
This statement tells Word that following any actions apply to the Selection.

[Application.]Selection

The Application.Selection property returns the Selection object that represents the current selection in a window or pane of 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

Checking the Selection

Sub...

  With Selection
    If .Type = wdSelectionShape Then

The 4th line checks if the selection is a Shape.

Selection.Type

The Selection.Type property returns a WdSelectionType value that represents the selection type.

obj-Word-Selection-Type

If the selection is a Shape then this property returns a wdSelectionShape (=8).

obj-Word-WdSelectionType-wdSelectionShape

Toggles the Lock Anchor

Sub...

  With Selection
    If .Type = wdSelectionShape Then
      .ShapeRange.LockAnchor = wdToggle

The 5th line toggles the ShapeRange.LockAnchor property value.

Selection.ShapeRange

The Slection.ShapeRange property returns the ShapeRange collection object that represents all the Shape objects in the selection.

obj-Word-Selection-ShapeRange

ShapeRange.LockAnchor

The ShapeRange.LockAnchor property returns True if the anchor for the ShapeRange object is locked to the anchoring paragraph.

obj-Word-ShapeRange-LockAnchor

And, the wdToggle is very interesting constant. This toggles a property’s value.
If the value is True, it switches to False.
If the value is False, it switches to True.

Ends the If and the With statement

Sub...

  With Selection
    If .Type = wdSelectionShape Then
      .ShapeRange.LockAnchor = wdToggle
    End If
  End With

The 6th line ends the If statement.
And, the 7th line ends the With statement.

After the 7th line evaluated, this macro ends.

Sub ToggleAnchorLock()

  With Selection
    If .Type = wdSelectionShape Then
      .ShapeRange.LockAnchor = wdToggle
    End If
  End With

End Sub

Properties

http://www.relief.jp/itnote/archives/002538.php

Apply To

  • Word 2013
  • Word 2010
  • Word 2007