Hi All,
I have following mailmerge format in my word document along with few other tables before & after this. With doc.MailMerge.ExecuteWithRegions(ds.Tables[0]); it creates tables equal to the number of rows in datatable;
«TableStart:MyTable»
Name
«Name»
Age
«Age»
«TableEnd:MyTable»
Is there any way I can access only the tables created by the mail merge only?
Thanks In Advance,
KP King
Thanks for your inquiry. Maybe you can try using code like the following:
public void Test062()
{
// Create dymmy datasource
DataTable data = new DataTable("MyTable");
data.Columns.Add("Name");
data.Columns.Add("Age");
// Add few rows
data.Rows.Add(new object[]
{
"Den",
"25"
});
data.Rows.Add(new object[]
{
"Ben",
"45"
});
data.Rows.Add(new object[]
{
"Steve",
"30"
});
// Open template
Document doc = new Document(@"Test062\in.doc");
// Add MergeField event handler
MailMergeHelper helper = new MailMergeHelper();
doc.MailMerge.MergeField += new MergeFieldEventHandler(helper.MailMerge_GetTables);
// Execute mail merge
doc.MailMerge.ExecuteWithRegions(data);
// List contains tables filled during mail merge
// For example we can highlight text in these tables
foreach(Table tab in helper.TablesList)
{
// Get collection of runs
NodeCollection runs = tab.GetChildNodes(NodeType.Run, true);
foreach(Run run in runs)
{
run.Font.HighlightColor = Color.Yellow;
}
}
// Save docuement
doc.Save(@"Test062\out.doc");
}
class MailMergeHelper
{
public MailMergeHelper()
{
mTablesList = new List <Table> ();
}
public void MailMerge_GetTables(object sender, MergeFieldEventArgs e)
{
// Get table where current mergefield is located
Table currTable = e.Field.Start.GetAncestor(NodeType.Table) as Table;
// Add table to list
if (currTable != null && !mTablesList.Contains(currTable))
mTablesList.Add(currTable);
}
// Returns list of table filled durign mail merge
public List <Table> TablesList
{
get
{
return mTablesList;
}
}
private List <Table> mTablesList;
}
Thanks Alexey!! for your prompt reply,
This will really help me. But what I forgot to mention in my last query is I wan’t to access these tables when I open the “out.doc” file again not in same block of code.
Please let me know if there is any way I can achieve this.
I think you can just insert bookmark into each table and access tables in the document by bookmarks as I described here: https://forum.aspose.com/t/94012
You can use the following code to insert bookmarks.
// Insert bookmarks in each table
DocumentBuilder builder = new DocumentBuilder(doc);
int bkIndex = 0;
foreach(Table tab in helper.TablesList)
{
// move docuemnt builder cursor to the firt cell of the document
builder.MoveTo(tab.FirstRow.FirstCell.FirstParagraph);
// Insert bookmark
builder.StartBookmark(string.Format("tab_{0}", bkIndex));
builder.EndBookmark(string.Format("tab_{0}", bkIndex));
bkIndex++;
}
Hope this helps.
Best regards.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.