Replace bookmark text in Word file using Open XML SDK

Here’s my approach after using you guys as inspiration: IDictionary<String, BookmarkStart> bookmarkMap = new Dictionary<String, BookmarkStart>(); foreach (BookmarkStart bookmarkStart in file.MainDocumentPart.RootElement.Descendants<BookmarkStart>()) { bookmarkMap[bookmarkStart.Name] = bookmarkStart; } foreach (BookmarkStart bookmarkStart in bookmarkMap.Values) { Run bookmarkText = bookmarkStart.NextSibling<Run>(); if (bookmarkText != null) { bookmarkText.GetFirstChild<Text>().Text = “blah”; } }

OpenXML SDK having borders for cell

I recommend installing the Open XML 2.5 productivity tool. Then create a blank Excel document that contains the border and color you desire. Open that file in the productivity tool and then click reflect code. It will then give you the C# code that is required to produce that border and background color. The code … Read more

Save modified WordprocessingDocument to new file

If you use a MemoryStream you can save the changes to a new file like this: byte[] byteArray = File.ReadAllBytes(“c:\\data\\hello.docx”); using (MemoryStream stream = new MemoryStream()) { stream.Write(byteArray, 0, (int)byteArray.Length); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true)) { // Do work here } // Save the file with the new name File.WriteAllBytes(“C:\\data\\newFileName.docx”, stream.ToArray()); }