I have a C# (Windows Forms, .Net 3.5, C#2008) application used to create Word documents. The Word documents contain bookmarks that are replaced by the fields from my application (like name, zipcode, address etc).
If you need to use fields multiple times in a document Word has the capability to {REF bookmarkname} the bookmark.
However the way I use it the bookmark is replaced and disappears so the ref doesn't work. I do want the range to disappear because I have text in the document like <zipcode> that I want to replace.
An extract of the code I use:
object range = bookmark.GetType().InvokeMember
("Range", BindingFlags.GetProperty, null, bookmark, null);
object[] setRangeTextArgs = new object[1];
setRangeTextArgs[0] = "New text";
range.GetType().InvokeMember("Text", BindingFlags.SetProperty, null, range, setRangeTextArgs);
I've tried to find a way to preserve the bookmark and only replace the current text in the bookmark, but otherwise I do have a range object, and I can just recreate the bookmark!
Thanks in advance!