I have a word mailmerge template that our users create. In that template I have certain areas that need to be removed if there is no data related to that section in my dataset. I read in another post that the best way to handle this would be to set up the template with sections or use bookmarks. How do I go about deleting all of the contents within a paragraph or section? In some cases I would have tables that exist within a bookmark or section. I don’t want the table, text, spacing or anything to show in the bookmark or section. Can you give me an example of how I might accomplish this?
Thanks for your inquiry. I think that you should use sections. For example see the following code and attached documents.
// Open document Document doc = new Document(@"Test009\in.doc"); // Remove second section doc.Sections.Remove(); // Save document doc.Save(@"Test009\out.doc");
Hope this helps.
Thanks. I guess the only reason I’d like to use bookmarks is because our users will be creating the word templates. And you can give bookmarks a name that makes sense to them instead of 1,2,3,4. Is there a way to do the same thing with bookmarks. Thanks!
Also, the table tags in the word template (e.g. <>) takes up a space when I convert the merged document to a PDF file. Is there a way to prevent that?
Ok, so I got the section code to work but there is a possibility that I might remove 3 sections based on the data I get back from the database. I noticed that when you delete one section, the section numbers change in the word document. Is there a way to keep the section numbers static so I don’t have to right a routine to keep track of the changing sections numbers as I delete them?
First, see the following link. I think that you can find solution how you can use bookmarks.
Second, you can also use bookmarks in the sections. For example you can use this code.
Document doc = new Document("in.doc"); DocumentBuilder builder = new DocumentBuilder(doc); //Move to bookmark builder.MoveToBookmark("myFirstSection"); //Remove section builder.CurrentSection.Remove();
Hope this helps.
Yeah, I think it makes more sense to just stick with sections at this point. But I noticed that when you delete one section, the section numbers change in the word document. Is there a way to keep the section numbers static so I don’t have to keep track of the changing sections numbers as I delete them?
Just don’t stick with section numbers. If every section has a bookmark with some semantically meaningful name then you can navigate amongst the sections without worrying about their ordinal numbers. If you need somewhere these numbers you can obtain them using CurrentSection.