Article ID: 183866
Article Last Modified on 7/16/2007
176476 OFF: Office Assistant Not Answering Visual Basic Questions
Sub TypeTextMethod()
Dim MyText As String
MyText = "<Replace this with your text>"
Selection.TypeText (MyText)
End Sub
Sub RangeProperty()
' Range Example:
ActiveDocument.Range.Text = "Replaced"
End Sub
Sub InsertAfterMethod()
Dim MyText As String
Dim MyRange As Object
Set MyRange = ActiveDocument.Range
MyText = "<Replace this with your text>"
' Selection Example:
Selection.InsertAfter (MyText)
' Range Example:
' (Inserts text at the current position of the insertion point.)
MyRange.Collapse
MyRange.InsertAfter (MyText)
End Sub
InsertBefore Method example:
Sub InsertBeforeMethod()
Dim MyText As String
Dim MyRange As Object
Set MyRange = ActiveDocument.Range
MyText = "<Replace this with your text>"
' Selection Example:
Selection.InsertBefore (MyText)
' Range Example: Inserts text at the beginning
' of the active document.
MyRange.InsertBefore (MyText)
End Sub
Sub CommentsCollectionObject()
Dim MyText As String
Dim MyRange As Object
Set MyRange = ActiveDocument.Range
MyText = "<Replace this with your text>"
' Selection Example:
Selection.Comments.Add Range:=Selection.Range, Text:=MyText
' Range Example:
MyRange.Comments.Add Range:=Selection.Range, Text:=MyText
End Sub
Sub FieldsCollectionObject()
Dim MyText As String
Dim MyRange As Object
Set MyRange = Selection.Range
MyText = "<Replace this with your text>"
' 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
This example inserts a formula field. The result is formatted with a dollar
sign.
Sub InsertFormulaMethod()
Selection.InsertFormula Formula:="=100,000.0-45,000.0", _
NumberFormat:="$#,##0.0"
End Sub
Sub FormattedTextProperty()
' 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 HeaderFooterProperty()
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
The following example changes the text of both the primary header and the
primary footer for the first section of the active document.
Sub HeaderFooterObject()
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 InsertDateTimeMethod()
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 InsertParagraphMethod()
Dim MyRange As Object
Set MyRange = ActiveDocument.Range
' Selection Example:
Selection.InsertParagraph
' Range Example:
MyRange.Collapse Direction:=wdCollapseStart
MyRange.InsertParagraph
End Sub
Sub InsertSymbolMethod()
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()
Dim MyRange As Object
Set MyRange = Selection.Range
' Selection Example:
Selection.Paste
' Range Example:
MyRange.Collapse Direction:=wdCollapseStart
MyRange.Paste
End Sub
For more information about using the Range Object, from the Visual
Basic Editor, click the Office Assistant, type Range Object,
click Search, and then click to view "Range Object."
176476 OFF: Office Assistant Not Answering Visual Basic Questions
181058 OFF98: How to Run Sample Code from Knowledge Base Articles
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: wordcon vb vba vbe
Keywords: kbdtacode kbhowto kbmacroexample KB183866