We’ve got a template-system here based on word-templates which are merged using aspose.words. This works just great.
Now we have a special problem. We have a part in a document (template) that contains a paragraph (without merge-fields) followed by a table (only merge-fields). During mail-merge we have to check in the database-record for a flag. If this flag is set, then the paragraph and the table have to be deleted from the resulting file. (Please note: before and after this section with the paragraph and the table there are and might be many other paragraphs, tables, sections, etc.)
How can I do this?
I know I can work with sections, but as far as I know I don’t have a possibility to name sections. So I would have to get the section by index. But what if the template-admin (just a regular MS-Word-User) makes some changes to the template (e.g. inserting new sections), then this approach leads to problems.
Please use the following code snippet to achieve your requirements. Please find sample doc file and HandleMergeField code in attachment.
// Load the template document.
Document doc = new Document(MyDir + "Template.doc");
// Setup mail merge event handler to do the custom work.
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
// This is the data for mail merge.
String[] fieldNames = new String[] {"RecipientName", "SenderName", "FaxNumber", "PhoneNumber", "Subject", "Body", "Urgent", "ForReview", "PleaseComment"};
Object[] fieldValues = new Object[] {"Josh", "Jenny", "123456789", "", "Hello", "Test message 1", true, false, true};
// Execute the mail merge.
doc.MailMerge.Execute(fieldNames, fieldValues);
// Save the finished document.
doc.Save(MyDir + "Template Out.doc");
Thanks for your quick reply.
I’ll test the callback-method for deleting the table.
Unfortunately we have the problem, that before this table we have a paragraph containing some text explaining the content of the following table (in fact, the table can contain several records, so we use TableStart/TableEnd). This paragraph sould be deleted as well.
To make things even more complicated, we cannot be sure whether the paragraph with the explanation comes before or after the table and we don’t know how many sentences/paragraphs the explanation contains.
So, is there any way to mark some regions in a word-file in a way we can access with aspose.words so that we can delete it from the dom?
Thanks for your input.
For testing I marked the table and the paragraph as bookmark (named “TEST”) and then used the following code:
Dim doc As New Document(Server.MapPath("test.doc"))
Dim bookmark As Bookmark = doc.Range.Bookmarks("TEST")
bookmark.Text.Remove(0, bookmark.Text.Length)
doc.Save(Server.MapPath("test.pdf"), SaveFormat.Pdf)
But in the resulting PDF the text (paragraph and table) is still there…
I tried this and found, that “RemoveBookmarkWithContent” from Emanual just did what I wanted it to do.
So, the solution with bookmarks is a valid way for regular MS Word-Users to mark and name sections in a file. The option of deleting the whole content of a bookmark does the rest. My problem should be solved.