To update the value of bookmark in Word 2007 using C#, first we need to get
the zip package part for main office document and associated xml.
The bookmark node is represented in office xml as follows:
<w:p w:rsidR="00E1380B" w:rsidRDefault="00E1380B">
<w:bookmarkStart w:id="2" w:name="Bottom" />
<w:r>
<w:t>This is a bottom of the file.</w:t>
</w:r>
<w:r w:rsidR="00C95038">
<w:br />
<w:t>But not end of the document?</w:t>
</w:r>
<w:bookmarkEnd w:id="2" />
</w:p>
Each bookmark is uniquely identified by id.
As every bookmark has a start and end node, we need to update all the data contained within start and end nodes.
The text to update is contained within the text nodes(<w:t>)
//Get the bookmarkStart node - here partXml is the xml associated with the main office document zip package part
XmlElement bookmarkStartNode = (XmlElement)partXml.DocumentElement.SelectSingleNode("//w:bookmarkStart[@w:id='" + _id + "']", _namespaceManager);
//Get all the text nodes following the bookmark start node and also get the bookmarkEnd node with same id
XmlNodeList followingNodesList = bookmarkStartNode.SelectNodes(".//following::w:t | .//following::w:bookmarkEnd[@w:id='" + _id + "']", _namespaceManager);
bool firstTextNodeFound = false;
foreach (XmlElement el in followingNodesList)
{
//Update the value of first text node and remove all other text nodes falling in between bookmark start and end nodes
if (el.Name == "w:t")
{
if (firstTextNodeFound)
{
el.ParentNode.RemoveChild(el);
}
else
{
el.InnerText = value;
firstTextNodeFound = true;
}
}
else
{
//It is bookmark end node - so move out
break;
}
}
Finally save the updated xml document in the main office document zip package part.
partXml.Save(docPart.GetStream());
For details to the basic of office xml check the following links
http://openxmldeveloper.org/articles/1970.aspx
It provides the e-book about the OpenXMl markup for word 2007
http://msdn2.microsoft.com/hi-in/library/bb727374(en-us).aspx#ManipulatingWord2007OpenXMLFiles_SetaDocumentCustomProperty
It provides information about setting custom properties in word 2007 docs.
http://msdn.microsoft.com/msdnmag/issues/06/11/BasicInstincts/
Gives basic idea about word 2007 document internals.
http://msdn2.microsoft.com/en-us/library/bb266220.aspx#office2007wordfileformat_openpackagingconventionsforwordxmlfileformat
Walk through the new default file format for Microsoft Office Word 2007.
http://blogs.msdn.com/brian_jones/archive/2006/09/13/753096.aspx
Provides complete specification for Open Packaging Conventions and Markup Language Reference.
http://msdn2.microsoft.com/en-us/library/bb332455.aspx
Office Open XML Formats: Replacing PowerPoint 2007 Slide Images – It helped in finding a way to replace picture content control.
http://msdn2.microsoft.com/en-us/library/system.io.packaging.package.aspx
Details about the System.IO.Packaging.Package class