Hi Smriti
Range objects ALWAYS specify a location, they don't just return text, they map a document
location and return text from that location. I think it may help you to read up on the
Range object.
I hope this VB.Net code sample (Console project) illustrates it a little. It was created
using VS2003, Office XP with the Office XP PIA's::
Imports Microsoft.Office.Interop
Imports System.Windows.Forms
Module WordAppTest
Private Const APP_NAME As String = "Word Range Test"
Sub Main()
Dim appWord As Word.Application
Dim testDoc As Word.Document
Try
' Create Word instance
appWord = New Word.Application
appWord.Visible = True
' Create a test document make sure we can see it!
testDoc = appWord.Documents.Add(appWord.NormalTemplate.FullName, _
False, Word.WdDocumentType.wdTypeDocument, True)
' Text to add to document
Dim text1 As String = "The quick brown fox jumps over the lazy dog. " & _
"The quick brown fox jumps over the lazy dog."
Dim text2 As String = _
"Little Jack Horner sat in the corner eating his curds and whey."
' Create 4 paragraphs, 2 sentences in each
testDoc.Content.Text = text1 & ControlChars.CrLf & _
text1 & ControlChars.CrLf & text1 & ControlChars.CrLf & _
text1 & ControlChars.CrLf
' Set up a range object to the second paragraph
Dim rangeTest As Word.Range
rangeTest = testDoc.Paragraphs(2).Range
' Display the text mapped by the range object
MessageBox.Show(rangeTest.Text, APP_NAME)
' Replace the text of the second paragraph
rangeTest.Text = text2 & ControlChars.CrLf
' Move the range start character forward 1 and last character backwards 3
rangeTest.Start += 1
rangeTest.End -= 3
' Set the colour of the mapped text to red
rangeTest.Font.Color = Word.WdColor.wdColorRed
' Display the text mapped by the range object
MessageBox.Show(rangeTest.Text, APP_NAME)
Catch ex As Exception
MessageBox.Show(ex.Message, APP_NAME, MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Finally
' Destroy the Word reference variable but leave Word running
If Not testDoc Is Nothing Then testDoc = Nothing
If Not appWord Is Nothing Then appWord = Nothing
End Try
End Sub
End Module
HTH + Cheers - Peter