MailMerge using Bookmarks

Hi,

We used to use the mailmerge.execute method for our merging operations. However we have recently changed from using Word to generate documents with Merge fields to using a 3rd party editor to generate documents with bookmarks (the 3rd party editor doesn't support merge fields)

We have therefore implemented a mailmerge process using bookmarks and this works fine.

However, one of the nice bits of mail merge was the RemoveEmptyParagraphs property. Our process just leaves empty lines.

Is there any way of identifying which line(?) a bookmark is on? So that we can delete that line if it only contains the bookmark?

Regards,

Gary Woods.

Try using the following code:

ArrayList paragraphsToRemove = new ArrayList();

foreach(Bookmark bookmark in doc.Range.Bookmarks)

{

if (bookmark.Text.Trim() == "")

{

Node node = bookmark.BookmarkStart;

while (node.NodeType != NodeType.BookmarkEnd)

{

if (node is Paragraph)

{

paragraphsToRemove.Add(node);

}

if(node.IsComposite && ((CompositeNode)node).HasChildNodes)

{

node = ((CompositeNode)node).FirstChild;

}

else

{

if (node.NextSibling == null)

node = node.ParentNode;

node = node.NextSibling;

}

}

}

}

foreach(Paragraph paragraph in paragraphsToRemove)

{

paragraph.Remove();

}

Hi Vladimir,

I had worked out a solution after my post and it seems quite similar to yours. As below.

ArrayList parasToRemove = new ArrayList();

DocumentBuilder docBuild = new DocumentBuilder();

// Loop through the fields/bookmarks in the document

foreach(Aspose.Word.Bookmark bkmk in objDocument.Range.Bookmarks){

// Call the process field helper function

string elementData = ProcessField(objCaseDetails, objTemplate, objDocument, bkmk.Name, objParagraph);

string fieldName = (char)171 + bkmk.Name.Split(' ')[0] + (char)187;

objDocument.Range.Replace(fieldName,"",false,false);

if(elementData=="")

{

docBuild.MoveToBookmark(bkmk.Name);

Aspose.Word.Paragraph p = docBuild.CurrentParagraph;

if(((CompositeNode)p).FirstChild is BookmarkStart && ((CompositeNode)p).LastChild is BookmarkEnd)

{

if (((BookmarkStart)((CompositeNode)p).FirstChild).Name == bkmk.Name && ((BookmarkEnd)((CompositeNode)p).LastChild).Name == bkmk.Name)

{

parasToRemove.Add(p);

}

}

}

bkmk.Text = elementData;

}

foreach(Aspose.Word.Paragraph p in parasToRemove.ToArray()){

p.Remove();

}

But my code seems a little flaky in that it works on my test harness but not in our development code.

I will probably steal some of your code. :)

Thanks for your time & effort

Regards,

Gary.

Hi again Vladimir,

I have been investigating my problem a bit more and it appears that my code doesn't work on our development platform because the FirstChild and LastChild nodes are empty.

Running my test harness locally on my machine produces expected results, however on our web service we have different null values for these fields.

I have attached an image to show this. The watches are of exactly the same paragraph from the same document just running on the different platforms.

Regards,

Gary.

Please also find attached our source document.

Vladimir,

I apologise for wasting your time.

I noticed that I wasn't actually initiliasing my documentBuilder with our source doc. :(

Thnaks again for your time.

Regards,

Gary

Glad you’ve made it on your own. Please don’t hesitate to ask if you have further questions.