Word automation

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
 
G

Guest

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top