Skip to content

Commit f57271c

Browse files
committed
Add sample
1 parent 1094fa0 commit f57271c

5 files changed

Lines changed: 106 additions & 0 deletions

File tree

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.14.36518.9 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Insert-hyperlink-during-Mailmerge", "Insert-hyperlink-during-Mailmerge\Insert-hyperlink-during-Mailmerge.csproj", "{DAE76AF9-49AD-4C2E-8064-430DFD8B7726}"
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+
{DAE76AF9-49AD-4C2E-8064-430DFD8B7726}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{DAE76AF9-49AD-4C2E-8064-430DFD8B7726}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{DAE76AF9-49AD-4C2E-8064-430DFD8B7726}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{DAE76AF9-49AD-4C2E-8064-430DFD8B7726}.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 = {344A442C-CA70-4696-8016-6BA6E07458A3}
24+
EndGlobalSection
25+
EndGlobal
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>Insert_hyperlink_during_Mailmerge</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>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Syncfusion.DocIO.DLS;
2+
3+
namespace Insert_hyperlink_during_mailmerge
4+
{
5+
class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
// Open the template Word document
10+
WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"));
11+
// Subscribe to the MailMerge.MergeField event
12+
// This event is triggered whenever a merge field is processed during mail merge
13+
document.MailMerge.MergeField += MailMerge_MergeField;
14+
// Define the merge field names present in the template document
15+
string[] fieldNames = new string[] { "EmployeeId", "Name", "Phone", "City", "Contact" };
16+
// Define the values that will replace the merge fields during mail merge
17+
string[] fieldValues = new string[] { "1001", "Peter", "+122-2222222", "London", "peter@xyz.com" };
18+
//Execute Mail merge
19+
document.MailMerge.Execute(fieldNames, fieldValues);
20+
// Save the result document
21+
document.Save(Path.GetFullPath(@"../../../Output/output.docx"));
22+
}
23+
/// <summary>
24+
/// Event handler that customizes how merge fields are processed.
25+
/// </summary>
26+
/// <param name="sender">The source object raising the event (MailMerge engine).</param>
27+
/// <param name="args">Provides details about the current merge field being processed</param>
28+
private static void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
29+
{
30+
31+
// Check if the current merge field is "Contact", If Yes this field will be replaced with a hyperlink
32+
if (args.FieldName == "Contact")
33+
{
34+
// Create a new paragraph and append hyperlink,
35+
WParagraph paragraph = new WParagraph(args.Document);
36+
WField hyperlink = paragraph.AppendHyperlink(args.FieldValue.ToString(), "Click ME", HyperlinkType.WebLink) as WField;
37+
// Get the current merge field object being processed
38+
WField mergeField = args.CurrentMergeField as WField;
39+
// Ensure the merge field exists before replacing it
40+
if (mergeField != null)
41+
{
42+
// Get the paragraph that contains the merge field
43+
WParagraph ownerParagraph = mergeField.OwnerParagraph;
44+
// Insert the child entity (e.g., hyperlink) from the new paragraph into the original paragraph
45+
for (int i = 0; i < paragraph.ChildEntities.Count; i++)
46+
{
47+
int fieldIndex = ownerParagraph.ChildEntities.IndexOf(mergeField);
48+
ownerParagraph.ChildEntities.Insert(fieldIndex, paragraph.ChildEntities[i].Clone());
49+
}
50+
// Remove the original merge field from the paragraph
51+
ownerParagraph.ChildEntities.Remove(mergeField);
52+
}
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)