Article ID: 161407
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
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
173707 OFF97: 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 KB161407