Replacing Word Doc Hyperlinks using OpenXML SDK

When moving from the CTP version of the Office OpenXML SDK to the released version, the API changed in many significant ways.  One thing that changed was moving from strongly typed attribute values to just strings. Another change I had recently struggled over was related to the handling of Hyperlink relationships.

In the CTP version, there was a fairly intuitive method pair appropriately named doc.documentPart.AddHyperlinkRelationship(…) .  This went hand in hand with doc.documentPart.DeleteHyperlinkRelationship(…).  So, naturally if you needed to replace one hyperlink with another, you could simply delete the existing one and add a new one with the same relationship ID and you were golden.

In the Released version of the Office OpenXML SDK, the DeleteHyperlinkRelationship() method went mysteriously missing.  The documentation is silent on this front.  There still exists the pair of methods: doc.documentPart.AddExternalRelationship(…) and doc.documentPart.DeleteExternalRelationship(…), so what gives?  There must have been some underlying reason in the API for removing the deleteExternalRelationship method, but since I wasn’t able to find any info on it, here’s how to replace an existing hyperlink relationship:  (obviously, you’ll want to find the one you’re replacing first, so you have the Id)

var mdp = doc.MainDocumentPart;
This entry was posted in OpenXML, Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *