WD97: VB Macro Examples to Insert Text into a Document

Last reviewed: February 11, 1998
Article ID: Q161407
The information in this article applies to:
  • Microsoft Word 97 for Windows

SUMMARY

This article provides several Visual Basic for Applications macro examples that use the Selection property and the Range object to insert text into a document.

For more information about using the Range object, while in the Visual Basic for Applications Editor, click the Office Assistant, type "Range," click Search, and then click to view "Range Object."

For more information about using the Selection Object, while in the Visual Basic for Applications Editor, click the Office Assistant, type "Selection," click Search, and then click to view "Selection Object."

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

To test these examples, insert them into the General Declarations section of a Visual Basic for Applications module.

   Sub TypeTextMethod()
     'Selection object: Inserts the specified text. If the ReplaceSelection
     'property is True, the selection is replaced by the specified text. If
     'ReplaceSelection is False, the specified text is inserted before the
     'selection.
     Dim MyText As String
     MyText = "<Replace this with your text>"
     Selection.TypeText (MyText)
   End Sub

   Sub TextProperty()
     'Range or Selection object: Returns or sets the text in the specified
     'range or selection.
     Dim MyText As String
     MyText = "<Replace this with your text>"

     'Selection Example:
     Selection.Text = MyText
     'Range Example:
     'Using the Range object, the following example replaces the entire
     'contents of a document with the word "Replaced" regardless of the
     'current position of the insertion point.
     ActiveDocument.Range.Text = "Replaced"
   End Sub

   Sub InsertAfterMethod()
     'Range or Selection object: Inserts the specified text at the end of a
     'range or selection. After this method is applied, the range or
     'selection expands to include the new text.
     Dim MyText As String
     MyText = "<Replace this with your text>"
     Dim MyRange As Object
     Set MyRange = ActiveDocument.Range

     'Selection Example:
     Selection.InsertAfter (MyText)
     'Range Example: Inserts text at the current position of the insertion
     'point.
     MyRange.Collapse
     MyRange.InsertAfter (MyText)
   End Sub

   Sub InsertBeforeMethod()
     'Range or Selection object: Inserts the specified text at the
     'beginning of a range or selection. After this method is applied, the
     'range or selection expands to include the new text.
     Dim MyText As String
     MyText = "<Replace this with your text>"
     Dim MyRange As Object
     Set MyRange = ActiveDocument.Range

     'Selection Example:
     Selection.InsertBefore (MyText)
     'Range Example: Inserts text at the beginning of the active document.
     MyRange.InsertBefore (MyText)
   End Sub

   Sub CommentsCollectionObject()
     'Range or Selection object: Inserts a comment at the current position
     'of the insertion point.
     Dim MyText As String
     MyText = "<Replace this with your text>"
     Dim MyRange As Object
     Set MyRange = ActiveDocument.Range

     'Selection Example:
     Selection.Comments.Add Range:=Selection.Range, Text:=MyText
     'Range Example:

     MyRange.Comments.Add Range:=Selection.Range, Text:=MyText
   End Sub

   Sub FieldsCollectionObject()
     'Range or Selection object: Inserts a field at the current position of
     'the insertion point.
     Dim MyText As String
     MyText = "<Replace this with your text>"
     Dim MyRange As Object
     Set MyRange = Selection.Range

     'Selection Example:
     Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldQuote, _
     Text:=MyText
     'Range Example:
     Range.Fields.Add Range:=Selection.Range, Type:=wdFieldQuote, _
     Text:=MyText
   End Sub

   Sub FormattedTextProperty()
     'This property returns a Range object with the character formatting
     'and text from the specified range or selection. Paragraph formatting
     'is included in the Range object if there is a paragraph mark in the
     'range or selection. When you set this property, the text in the range
     'is replaced with formatted text. If you do not want to replace the
     'existing text, use the Collapse method before using this property.

     'This example copies the first paragraph in the document, including
     'its formatting, and inserts the formatted text at the insertion
     'point.
     Selection.Collapse Direction:=wdCollapseStart
     Selection.FormattedText = ActiveDocument.Paragraphs(1).Range
   End Sub

   Sub HeaderFooterObject()
     'The following example changes the text of both the primary header and
     'the primary footer for the first section of the active document.
     Dim MyText As String
     MyHeaderText = "<Replace this with your text>"
     MyFooterText = "<Replace this with your text>"
     With ActiveDocument.Sections(1)
       .Headers(wdHeaderFooterPrimary).Range.Text = MyHeaderText
       .Footers(wdHeaderFooterPrimary).Range.Text = MyFooterText
     End With
   End Sub

   Sub HeaderFooterProperty()
     'The HeaderFooter property requires that the selection be located
     'within a header or footer or an error will occur.
     Dim MyText As String
     MyText = "<Replace this with your text>"
     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
     Selection.HeaderFooter.Range.Text = "MyText"
     ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
   End Sub

   Sub InsertDateTimeMethod()
     'Range or Selection object: This example inserts a Time field for the
     'current date. A possible result might be "November 18, 1996."
     Dim MyRange As Object
     Set MyRange = Selection.Range

     'Selection Example:
     Selection.InsertDateTime DateTimeFormat:="MMMM dd, yyyy", _
     InsertAsField:=True
     'Range Example:
     MyRange.InsertDateTime DateTimeFormat:="MMM dd, yyyy", _
     InsertAsField:=True
   End Sub

   Sub InsertFormulaMethod()
     'This example inserts a formula field. The result is formatted with a
     'dollar sign.
     Selection.InsertFormula Formula:="=100,000.0-45,000.0", _
     NumberFormat:="$#,##0.0"
   End Sub

   Sub InsertParagraphMethod()
     'Range or Selection object: This example inserts a new paragraph below
     'the current position of the insertion point.
     Dim MyRange As Object
     Set MyRange = ActiveDocument.Range

     'Selection Example:
     Selection.InsertParagraph
     'Range Example:
     MyRange.Collapse Direction:=wdCollapseStart
     MyRange.InsertParagraph
   End Sub

   Sub InsertSymbolMethod()
     'Range or Selection object: This example inserts a double-headed arrow
     'at the insertion point.
     Dim MyRange As Object
     Set MyRange = ActiveDocument.Range

     'Selection Example:
     Selection.InsertSymbol CharacterNumber:=171, Font:="Symbol", _
     Unicode:=False
     'Range Example:
     MyRange.Collapse Direction:=wdCollapseStart
     MyRange.InsertSymbol CharacterNumber:=171, Font:="Symbol", _
     Unicode:=False
   End Sub

   Sub PasteMethod()
     'Range or Selection object: This example inserts text placed on the
     'clipboard at the current position of the insertion point.
     Dim MyRange As Object
     Set MyRange = Selection.Range

     'Selection Example:
     Selection.Paste
     'Range Example:
     MyRange.Collapse Direction:=wdCollapseStart
     MyRange.Paste
   End Sub

For information about how to do this in earlier versions of Word, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q86079
   TITLE     : Inserting Macro Variable Contents into a Document Window


Additional query words: word8 word97 8.0
Keywords : kbwordvba kbmacro kbusage
Version : 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 11, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.