Word VBA: Deleting All Text

This Word VBA macro deletes the entire contents of an active document body.

Macro Example

Sub DeleteAllText_Document()
  
  ActiveDocument.StoryRanges(wdMainTextStory).Delete

End Sub

Description

[Application.]ActiveDocument

  ActiveDocument...

The [Application.]ActiveDocument property returns an active Document object.

obj-Word-Application-ActiveDocument

Because the ActiveDocument property is a member of the Word.Global class, we can omit the Application property.

obj-Word-ActiveDocument

ActiveDocument.StoryRanges (Document.StoryRanges)

  ActiveDocument.StoryRanges...

The ActiveDocument.StoryRanges (Document.StoryRanges property) returns the StoryRanges collection object that represents all the stories in the active document

obj-Word-Document-StoryRanges

StoryRanges[.Item](wdMainTextStory)

  ActiveDocument.StoryRanges(wdMainTextStory)...

The StoryRanges.Item method returns a single story of a range or selection as a Range object.

obj-Word-StoryRanges-Item

And the StoryRanges.Item(wdMainTextStory) represents the main text story.

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

StoryRanges(wdMainTextStory).Delete (Range.Delete)

  ActiveDocument.StoryRanges(wdMainTextStory).Delete

Finally, the StoryRanges(wdMainTextStory).Delete (Range.Delete method) deletes the main text story range.

obj-Word-Range-Delete

Sub DeleteAllText_Document()
  
  ActiveDocument.StoryRanges(wdMainTextStory).Delete

End Sub

Properties

http://www.relief.jp/itnote/archives/word-vba-remove-all-sentences.php

Apply To

  • Word 2013
  • Word 2010
  • Word 2007