@@ -15,7 +15,7 @@ static void Main(string[] args)
1515 //Replace Bookmark name
1616 ReplaceBookmarkName ( document , "Northwind" , "New_Bookmark" ) ;
1717 //Creates file stream.
18- using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"Output/Output .docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
18+ using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"Output/Result .docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
1919 {
2020 //Saves the Word document to file stream.
2121 document . Save ( outputFileStream , FormatType . Docx ) ;
@@ -28,31 +28,64 @@ static void Main(string[] args)
2828 /// Replace bookmark name
2929 /// </summary>
3030 /// <param name="document">Input Word document.</param>
31- /// <param name="existingbookmarkName ">The name of the bookmark to replace.</param>
31+ /// <param name="existingBookmarkName ">The name of the bookmark to replace.</param>
3232 /// <param name="replaceBookmarkName">The new name for the bookmark.</param>
33-
34- private static void ReplaceBookmarkName ( WordDocument document , string existingbookmarkName , string replaceBookmarkName )
33+ private static void ReplaceBookmarkName ( WordDocument document , string existingBookmarkName , string replaceBookmarkName )
3534 {
3635 //Gets the bookmark instance by using FindByName method of BookmarkCollection with bookmark name
37- Bookmark bookmark = document . Bookmarks . FindByName ( existingbookmarkName ) ;
36+ Bookmark bookmark = document . Bookmarks . FindByName ( existingBookmarkName ) ;
3837 // No bookmark found, return immediately
3938 if ( bookmark == null )
4039 return ;
41- //Gets owner paragraph of the bookmark
42- WParagraph bookmarkStartParagraph = bookmark . BookmarkStart . Owner as WParagraph ;
43- //Gets index of the bookmark start
44- int boomarkStartIndex = bookmarkStartParagraph . ChildEntities . IndexOf ( bookmark . BookmarkStart ) ;
45- //Gets bookmark end paragraph
46- WParagraph bookmarkEndParagraph = bookmark . BookmarkEnd . Owner as WParagraph ;
47- //Gets index of the bookmark end
48- int boomarkEndIndex = bookmarkEndParagraph . ChildEntities . IndexOf ( bookmark . BookmarkEnd ) ;
40+ // Variables for owner entity start and end positions
41+ WParagraph startParagraph = null ;
42+ InlineContentControl startICC = null ;
43+ int startIndex = - 1 ;
44+
45+ WParagraph endParagraph = null ;
46+ InlineContentControl endICC = null ;
47+ int endIndex = - 1 ;
48+ // Determine the owner and index for the bookmark start.
49+ // The bookmark start may be inside a WParagraph (as a child entity)
50+ // or inside an InlineContentControl (as a paragraph item).
51+ if ( bookmark . BookmarkStart != null && bookmark . BookmarkStart . Owner is WParagraph )
52+ {
53+ startParagraph = bookmark . BookmarkStart . Owner as WParagraph ;
54+ startIndex = startParagraph . ChildEntities . IndexOf ( bookmark . BookmarkStart ) ;
55+ }
56+ else if ( bookmark . BookmarkStart != null && bookmark . BookmarkStart . Owner is InlineContentControl )
57+ {
58+ startICC = bookmark . BookmarkStart . Owner as InlineContentControl ;
59+ startIndex = startICC . ParagraphItems . IndexOf ( bookmark . BookmarkStart ) ;
60+ }
61+ // Determine the owner and index for the bookmark end.
62+ // Similar to start, the end could be in a paragraph or inline content contro
63+ if ( bookmark . BookmarkEnd != null && bookmark . BookmarkEnd . Owner is WParagraph )
64+ {
65+ endParagraph = bookmark . BookmarkEnd . Owner as WParagraph ;
66+ endIndex = endParagraph . ChildEntities . IndexOf ( bookmark . BookmarkEnd ) ;
67+ }
68+ else if ( bookmark . BookmarkEnd != null && bookmark . BookmarkEnd . Owner is InlineContentControl )
69+ {
70+ endICC = bookmark . BookmarkEnd . Owner as InlineContentControl ;
71+ endIndex = endICC . ParagraphItems . IndexOf ( bookmark . BookmarkEnd ) ;
72+ }
4973 //Removes the bookmark from Word document.
5074 document . Bookmarks . Remove ( bookmark ) ;
51- //Inserts new bookmark start in place of deleted bookmark
52- bookmarkStartParagraph . ChildEntities . Insert ( boomarkStartIndex , bookmarkStartParagraph . AppendBookmarkStart ( replaceBookmarkName ) ) ;
53- //Inserts bookmark end in place of deleted bookmark
54- bookmarkEndParagraph . ChildEntities . Insert ( boomarkEndIndex , bookmarkEndParagraph . AppendBookmarkEnd ( replaceBookmarkName ) ) ;
55-
75+ // Create a new BookmarkStart and insert at the recorded index.
76+ BookmarkStart newBookmarkStart = new BookmarkStart ( document , replaceBookmarkName ) ;
77+ // Insert new bookmark start at the original position with the new name.
78+ if ( startParagraph != null )
79+ startParagraph . ChildEntities . Insert ( startIndex , newBookmarkStart ) ;
80+ else if ( startICC != null )
81+ startICC . ParagraphItems . Insert ( startIndex , newBookmarkStart ) ;
82+ // Create a new BookmarkEnd and insert at the recorded index.
83+ BookmarkEnd newBookmarkEnd = new BookmarkEnd ( document , replaceBookmarkName ) ;
84+ // Insert new bookmark end at the original position with the new name.
85+ if ( endParagraph != null )
86+ endParagraph . ChildEntities . Insert ( endIndex , newBookmarkEnd ) ;
87+ else if ( endICC != null )
88+ endICC . ParagraphItems . Insert ( endIndex , newBookmarkEnd ) ;
5689 }
5790 #endregion
5891 }
0 commit comments