Skip to content

Commit 820fb10

Browse files
committed
Modified the Mail merge sample
1 parent e80e259 commit 820fb10

File tree

2 files changed

+9
-6
lines changed
  • Mail-Merge/Execute-GroupMailMerge-FirstOccurrenceOnly/.NET/Execute-GroupMailMerge-FirstOccurrenceOnly
  • Word-document/Extract-images-from-Word-document/.NET-Framework/Extract-images-from-Word-document

2 files changed

+9
-6
lines changed

Mail-Merge/Execute-GroupMailMerge-FirstOccurrenceOnly/.NET/Execute-GroupMailMerge-FirstOccurrenceOnly/Program.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,29 @@ static void Main()
3434
// Find all merge fields with the same name
3535
List<Entity> mergeGroups = document.FindAllItemsByProperty(EntityType.MergeField, "FieldName", groupName);
3636
// Start from second occurrence to remove duplicates
37-
for (int i = 1; i < mergeGroups.Count; i++)
37+
for (int i = 2; i < mergeGroups.Count; i++)
3838
{
3939
WMergeField mergeField = mergeGroups[i] as WMergeField;
4040
// Check if it's a group start field
41-
if (mergeField.FieldCode.Contains("TableStart") || mergeField.FieldCode.Contains("BeginGroup"))
41+
if (mergeField.FieldCode.Contains("TableStart:") || mergeField.FieldCode.Contains("BeginGroup:"))
4242
{
43+
// Generate unique Bookmark name
44+
string bkmkGroupName = groupName + Guid.NewGuid().ToString();
4345
// Add bookmark start before the group
44-
BookmarkStart bkmkStart = new BookmarkStart(document, groupName);
46+
BookmarkStart bkmkStart = new BookmarkStart(document, bkmkGroupName);
4547
WParagraph startPara = mergeField.OwnerParagraph;
4648
int mergeFieldIndex = startPara.ChildEntities.IndexOf(mergeField);
4749
startPara.ChildEntities.Insert(mergeFieldIndex, bkmkStart);
4850
// Add bookmark end after the group
4951
WMergeField endField = mergeGroups[i + 1] as WMergeField;
50-
BookmarkEnd bkmkEnd = new BookmarkEnd(document, groupName);
52+
BookmarkEnd bkmkEnd = new BookmarkEnd(document, bkmkGroupName);
5153
WParagraph endPara = endField.OwnerParagraph;
5254
int endFieldIndex = endPara.ChildEntities.IndexOf(endField);
5355
endPara.ChildEntities.Insert(endFieldIndex + 1, bkmkEnd);
56+
i++;
5457
// Delete content inside the bookmark
5558
BookmarksNavigator navigator = new BookmarksNavigator(document);
56-
navigator.MoveToBookmark(groupName);
59+
navigator.MoveToBookmark(bkmkGroupName);
5760
navigator.DeleteBookmarkContent(false);
5861
document.Bookmarks.Remove(navigator.CurrentBookmark);
5962
// Remove owner table if applicable

Word-document/Extract-images-from-Word-document/.NET-Framework/Extract-images-from-Word-document/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class Program
1414
{
1515
static void Main(string[] args)
1616
{
17-
// Open the file as a stream.
17+
// Open the Word document.
1818
using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../Data/Template.docx")))
1919
{
2020
// Find all pictures by EntityType in the Word document.

0 commit comments

Comments
 (0)