Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
Posted
Comments
PhilLenoir 19-Aug-14 10:41am    
I've not used the .NET libraries for this, but my technique is always to record a macro to do pretty much what you want in Word and go to the VBA. Grab that code and edit it C# to refine the behaviour and ensure your references are OK.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900