Free Support Forum - aspose.com

Merge with regions and Bookmarks

Hello,

I'm trying to perform a merge-with-regions and there are some bookmarks inside my region. I want to copy the bookmarks as well. The reason I want to copy the bookmarks is that I need to use some application logic to delete selected instance of these internal bookmarks.

The problem is that everything between my <> and <> mergefields gets copied except for the nested bookmarks.

Is there a way to copy bookmarks (with new names), or is there a better approach.

Please help.

Thanks in advance.


This message was posted using Page2Forum from Mail Merge with Regions Explained - Aspose.Words for .NET and Java

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can rename bookmarks during MailMerge using MergeField event handler. For example see the following code.

public void Test060()

{

//Create DataSourse

DataTable table = new DataTable("myTable");

table.Columns.Add("Field1");

for (int i = 0; i < 10; i++)

{

DataRow row = table.NewRow();

row[0] = "kjsdf;jshd;fkjs";

table.Rows.Add(row);

}

//Open document

Document doc = new Document(@"Test060\in.doc");

//Add MergeField event handler

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_060);

doc.MailMerge.ExecuteWithRegions(table);

doc.Save(@"Test060\out.doc");

}

//Index of bookmark

int bookmarkIndex = 0;

void MailMerge_MergeField_060(object sender, MergeFieldEventArgs e)

{

if (e.FieldName == "field1")

{

//Get parent node of mergefiled

CompositeNode node = e.Field.Start.ParentParagraph;

while (node != null)

{

//Get bookmarkstart collection

NodeCollection starts = node.GetChildNodes(NodeType.BookmarkStart, true);

if (starts.Count > 0)

{

//Rename bookamrks

foreach (BookmarkStart start in starts)

{

start.Bookmark.Name = start.Bookmark.Name + bookmarkIndex.ToString();

bookmarkIndex++;

}

}

if (node.NextSibling.IsComposite)

{

node = (CompositeNode)node.NextSibling;

}

else

{

break;

}

}

}

}

I hope this could help you. Also you can remove content from region using MergeFiled event handler.

Best regards.

Thankyou Alexey.