{"id":1329,"date":"2013-01-14T19:21:49","date_gmt":"2013-01-14T19:21:49","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1329"},"modified":"2013-01-14T19:21:49","modified_gmt":"2013-01-14T19:21:49","slug":"word-openxml","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2013\/01\/14\/word-openxml\/","title":{"rendered":"Working with Word and OpenXML"},"content":{"rendered":"<p>When working with Word documents using the <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=5124\" title=\"Open XML SDK 2.0 for Microsoft Office\" target=\"_blank\">OpenXML SDK<\/a>, it&#8217;s not obvious how to remove document protection.  The following code shows you how:<\/p>\n<pre lang=\"csharp\">\r\nusing (WordprocessingDocument wordDocument = WordprocessingDocument.Open(\"[path to Word document]\", true))\r\n{\r\n\tDocumentFormat.OpenXml.OpenXmlElement protectedDocElement = wordDocument.MainDocumentPart.DocumentSettingsPart.ChildElements.FirstOrDefault(el => el.XmlQualifiedName.Name.Equals(\"documentProtection\"));\r\n\tif (protectedDocElement != null)\r\n\t{\r\n\t\tprotectedDocElement.Remove();\r\n\t}\r\n\t\r\n\twordDocument.MainDocumentPart.Document.Save();\r\n}\r\n<\/pre>\n<p>Following on from that, it&#8217;s also straight forward enough to remove macros from a document (or template):<br \/>\n<!--more--><\/p>\n<pre lang=\"csharp\">\r\nusing (WordprocessingDocument wordDocument = WordprocessingDocument.Open(\"[path to Word document]\", true))\r\n{\r\n\tVbaProjectPart vbaProjectPart = wordDocument.MainDocumentPart.VbaProjectPart;\r\n    if (vbaProjectPart != null)\r\n    {\r\n\t\twordDocument.MainDocumentPart.DeletePart(vbaProjectPart);\r\n\t}\r\n\t\r\n\twordDocument.MainDocumentPart.Document.Save();\r\n}\r\n<\/pre>\n<p>and finally, how to open a Word document stored in a SharePoint library and persist any changes made:<\/p>\n<pre lang=\"csharp\">\r\nusing (MemoryStream memoryStream = new MemoryStream())\r\n{\r\n\tbyte[] fileBinary = spFile.OpenBinary();\r\n\tmemoryStream.Write(fileBinary, 0, fileBinary.Length);\r\n\r\n\tusing (WordprocessingDocument wordDocument = WordprocessingDocument.Open(memStream, true))\r\n\t{\r\n\t\t\/\/ ...\r\n\t\t\r\n\t\twordDocument.MainDocumentPart.Document.Save();\r\n\t}\r\n\t\r\n\t\/\/ Save changes back to document\r\n\tspFile.SaveBinary(memoryStream);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When working with Word documents using the OpenXML SDK, it&#8217;s not obvious how to remove document protection. The following code shows you how: using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(&#8220;[path to Word document]&#8221;, true)) { DocumentFormat.OpenXml.OpenXmlElement protectedDocElement = wordDocument.MainDocumentPart.DocumentSettingsPart.ChildElements.FirstOrDefault(el => el.XmlQualifiedName.Name.Equals(&#8220;documentProtection&#8221;)); if &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2013\/01\/14\/word-openxml\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[55,3],"tags":[89,56,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-lr","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1329"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1329"}],"version-history":[{"count":3,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1329\/revisions"}],"predecessor-version":[{"id":1332,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1329\/revisions\/1332"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}