Title: Server-side word automation
Tags: c#;word-automation
Question: I am looking for alternatives to using openxml for a server-side word automation project. Does anyone know any other ways that have features to let me manipulate word bookmarks and tables?

Comment: @JohnBaum you should have mentioned this in the question itself..

Comment: What are you trying to automate and why does openxml not fit your needs, without those details it will be difficult to provide other options.

Comment: Now knowing what you want to do see this question it is basically the same thing you want to do. http://stackoverflow.com/questions/3308299/replace-bookmark-text-in-word-file-using-open-xml-sdk

Comment: i am trying to delete all the text within a bookmark start and bookmarkend. openxml doesnt let me do this as far as i know

Comment: that method does not work for me. The next sibling after my bookmark is not the only element part of the actual bookmark in the doc.

Here is the accepted answer: I am currently doing a project of developing a word automation project for my company and I am using DocX Very simple and straight forward API to work with. The approach I am using is, whenever I need to work with XML directly, this API has a property named "xml" in the Paragraph class which gives you access to the underlying xml direclty so that I can work with it. The best part is its not breaking the xml and not corrupting the resulting document. Hope this helps!

Example code using DocX..

``` XNamespace ns = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
    using(DocX doc = DocX.Load(@"c:\temp\yourdoc.docx"))
    {
         foreach( Paragraph para in doc.Paragraphs )
         {
             if(para.Xml.ToString().Contains("w:Bookmark"))
             {
                 if(para.Xml.Element(ns + "BookmarkStart").Attribute("Name").Value == "yourbookmarkname")
                  {
                          // you got to your bookmark, if you want to change the text..then 
                          para.Xml.Elements(ns + "t").FirstOrDefault().SetValue("Text to replace..");
                  }
             }
         }
    }
```

Alternative API exclusively to work with bookmarks is .. http://simpleooxml.codeplex.com/

Example on how to delete text from bookmarkstart to bookmarkend using this API..

``` MemoryStream stream = DocumentReader.Copy(string.Format("{0}\\template.docx", TestContext.TestDeploymentDir));
 WordprocessingDocument doc = WordprocessingDocument.Open(stream, true);
 MainDocumentPart mainPart = doc.MainDocumentPart;

 DocumentWriter writer = new DocumentWriter(mainPart);

 //Simply Clears all text between bookmarkstart and end
 writer.PasteText("", "YourBookMarkName");


 //Save to the memory stream, and then to a file
 writer.Save();

 DocumentWriter.StreamToFile(string.Format("{0}\\templatetest.docx", GetOutputFolder()), stream);
```

Loading the word document into different API's from memory stream.

```//Loading a document file into memorystream using SimpleOOXML API
MemoryStream stream = DocumentReader.Copy(@"c\template.docx");

//Opening it from the memory stream as OpenXML document
WordprocessingDocument doc = WordprocessingDocument.Open(stream, true);

//Opening it as DocX document for working with DocX Api
DocX document = DocX.Load(stream); 
```

Comment for this answer: not out of the box.. but you can get hold of it from the underlying xml directly..depending on your requirement..
Comment for this answer: thats what PasteText method is doing.. writer.PasteText("write some text", "yourBookMarkName"); .. as simple as that..
Comment for this answer: You can use it to erase all the lines spanning that bookmark using PasteText method, and its easy to use OpenXml to instert a table isn't it?
Comment for this answer: yes..I agree. that is a potential problem unless you work with  memory streams or save and open the same file within the program separately for achieving different functionality..
Comment for this answer: Both the api's mentioned above support loading directly from a stream, so i think its easy to work with them by passing the document as a memory stream between methods..
Comment for this answer: DocumentReader is class from SimpleOOXML APi, did you reference it properly in your solution?
Comment for this answer: DocumentReader is a helper class in SimpleOOXML api to help loading the document, you don't need it with DocX API, you can just load the document like - DocX.Load(@"c:\yourdoc.docx");
Comment for this answer: Oh.. But thats just a normal stream object, you can just use FileStream from System.IO to do that .. like - FileStream  
stream = new FileStream(@"c:\yourdoc.docx", FileMode.Open);
Comment for this answer: Yes.. did you also try SimpleOOXMl, as it is more specific to working with bookmarks, did you try using the PasteText Method in it...its working exactly how you are intending to do with your case..
Comment for this answer: Try commenting out these two line - in CleanNodes() method - var start = _doc.Xml.Descendants(ns + "bookmarkStart").ToList();
var end = _doc.Xml.Descendants(ns + "bookmarkEnd").ToList();
Comment for this answer: I leave the office by 5 (GMT) and that's when u r free :( I guess the code is just working fine and is a bit noddy and most straight forward to what u r looking for. Just refactor it to your needs. All the best..Cheers :)
Comment for this answer: am i able to access the bookmarks in the word doc using docX?
Comment for this answer: can you give an example on how i can use the xml to get at the bookmarks
Comment for this answer: Does this api support finding a bookmark and writing some text directly following it? THats another one of the things i need to accomplish
Comment for this answer: ok, my bookmark is a selection of 3 lines. Based on some criteria i have to either erase all the bookmark selection of lines or enter a new line after the bookmark and write a table there. Which of these two tasks does parse text accomplish?
Comment for this answer: well i was trying to avoid having to use two different api's and have one single solution.
Comment for this answer: if i use two seperate apis, i have difficulty saving as openxml opens the file and docx also opens the file but both cant save to it
Comment for this answer: i actually cant get access to the memory stream and docwriter for the docx api. Is there something i have to iport?
Comment for this answer: Thanks for updating the answer. One thing that i still cant get to work is the documentreader. It cant find that reference and i dont know how to get visual studio to recognize it?
Comment for this answer: I want to use just the docx api. How can i get access to the document reader/writer?
Comment for this answer: i tried this approach just now to replace the bookmark text and it didnt appear to do anything. Did it work in your test?
Comment for this answer: yes i am using the simpleooxml and the pastetext method. It doesnt seem to find the bookmark when i enter the bookmarkname. It says "Sequence contains no elements"
Comment for this answer: let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9234/discussion-between-john-baum-and-flowerking)
Comment for this answer: the bookmark deletion works now. The inserting a table at a specific point however does not.
Comment for this answer: let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9328/discussion-between-john-baum-and-flowerking)


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:03/21/12', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<c#><word-automation>', 'Title': 'Server-side word automation', 'ViewCount': 1785, 'id': 9810718, 'user_AboutMe': None, 'user_AccountId': 1225804.0, 'user_DisplayName': 'John Baum', 'user_Reputation': 3045.0, 'user_Views': 380.0, 'user_id': 1191353.0}}