Skip to content

Commit cc073d6

Browse files
Merge pull request #267 from VijayadharshiniMathiyalagan/ES-884992-How-to-skip-a-range-of-records-during-mail-merge-in-Word-document
Added sample to skip a range of records during mail-merge
2 parents 146e2bf + 3873789 commit cc073d6

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Skip-range-of-records", "Skip-range-of-records\Skip-range-of-records.csproj", "{3BADC57D-9594-49A2-92B3-A9925C41E66E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3BADC57D-9594-49A2-92B3-A9925C41E66E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3BADC57D-9594-49A2-92B3-A9925C41E66E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3BADC57D-9594-49A2-92B3-A9925C41E66E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3BADC57D-9594-49A2-92B3-A9925C41E66E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {DFC8B131-CCB4-495D-B062-BB8386F39BF6}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Data;
4+
5+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
6+
{
7+
// Opens the template document.
8+
using (WordDocument document = new WordDocument(fileStream, FormatType.Docx))
9+
{
10+
// Uses the mail merge events to skip a range of records during mail merge.
11+
document.MailMerge.MergeField += new MergeFieldEventHandler(SkipRangeOfRecords);
12+
// Executes Mail Merge with groups.
13+
document.MailMerge.ExecuteGroup(GetDataTable());
14+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
15+
{
16+
// Saves the Word document to file stream.
17+
document.Save(outputStream, FormatType.Docx);
18+
}
19+
}
20+
}
21+
22+
#region Helper methods
23+
/// <summary>
24+
/// Event handler to skip a range of records during mail merge.
25+
/// </summary>
26+
void SkipRangeOfRecords(object sender, MergeFieldEventArgs args)
27+
{
28+
// Check if the current row index is between 2 and 7.
29+
// If so, set the field text to an empty string, effectively skipping those records.
30+
if (args.RowIndex > 2 && args.RowIndex < 7)
31+
args.Text = string.Empty;
32+
}
33+
34+
/// <summary>
35+
/// Gets a DataTable populated with employee data for mail merge.
36+
/// </summary>
37+
DataTable GetDataTable()
38+
{
39+
// Create a new DataTable with columns for employee name and number.
40+
DataTable dataTable = new DataTable("Employee");
41+
dataTable.Columns.Add("EmployeeName");
42+
dataTable.Columns.Add("EmployeeNumber");
43+
44+
// Populate the DataTable with 20 rows of employee data.
45+
for (int i = 0; i < 20; i++)
46+
{
47+
// Create a new DataRow and add it to the DataTable.
48+
DataRow datarow = dataTable.NewRow();
49+
dataTable.Rows.Add(datarow);
50+
// Set employee name.
51+
datarow[0] = "Employee" + i.ToString();
52+
// Set employee number.
53+
datarow[1] = "EMP" + i.ToString();
54+
}
55+
// Return the populated DataTable.
56+
return dataTable;
57+
}
58+
59+
#endregion
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Skip_range_of_records</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>

0 commit comments

Comments
 (0)