Word automation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I have a word document(template) with some BookMarks. Also for the first time I have inserted some text against those bookmarks in the document through my code and have saved the document. For the next time I need to open the saved document with the text in bookmarks and before opening I may need to overwrite with the fresh values on some of the bookmarks.
When I tried to do this using Range.text or Range.InsertAfter methods it always appends with the existing text
Where as I want to replace the old text with the new one for that bookmark
Please let me know what is the solution for this

Regards
 
Hi Veda
Here is test code i have implemented

==================================================

private Microsoft.Office.Interop.Word.Application app = null

private void button1_Click_1(object sender, System.EventArgs e

app = new Microsoft.Office.Interop.Word.Application()
object optional = Missing.Value
object fileName = @"D:\Ramaprasad\Temp.rtf";

app.Documents.Open(ref fileName, ref optional,ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional)
MessageBox.Show("Opened")

BookMarkReplaceRange("Page1", "ramprasad")

app.ActiveDocument.Save()

app.Quit(ref optional,ref optional,ref optional)
System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
app = null


internal void BookMarkReplaceRange(string bookmarkName, string newText)
{
object missingValue = Type.Missing;
object oBookmarkName = bookmarkName;
Microsoft.Office.Interop.Word.Range rng = app.ActiveDocument.Bookmarks.get_Item(ref oBookmarkName).Range;
rng.Text = newText;
object oRng = rng;
app.ActiveDocument.Bookmarks.Add(bookmarkName, ref oRng);
}
 
Back
Top