@@ -40,34 +40,19 @@ private static void RemoveEditableRange(WordDocument document, string bookmarkNa
4040 BookmarksNavigator bookmarkNavigator = new BookmarksNavigator ( document ) ;
4141 // Move to the bookmark
4242 bookmarkNavigator . MoveToBookmark ( bookmarkName ) ;
43- // Get the content inside the bookmark
44- WordDocumentPart bookmarkContent = bookmarkNavigator . GetContent ( ) ;
45- // Loop through all sections in the bookmark content
46- for ( int s = 0 ; s < bookmarkContent . Sections . Count ; s ++ )
43+ // Get the bookmark content as word document
44+ WordDocument tempDoc = bookmarkNavigator . GetContent ( ) . GetAsWordDocument ( ) ;
45+ // Find all entities of type EditableRangeStart within the bookmark
46+ List < Entity > entity = tempDoc . FindAllItemsByProperty ( EntityType . EditableRangeStart , null , null ) ;
47+ // If any EditableRangeStart entities are found, iterate through them.
48+ if ( entity != null )
4749 {
48- WSection section = bookmarkContent . Sections [ s ] ;
49- // Iterate through all entities in the section body (paragraphs, tables, etc.).
50- for ( int i = 0 ; i < section . Body . ChildEntities . Count ; i ++ )
50+ foreach ( Entity item in entity )
5151 {
52- IEntity entity = section . Body . ChildEntities [ i ] ;
53-
54- if ( entity is WParagraph )
55- {
56- WParagraph paragraph = entity as WParagraph ;
57- // Loop through all child entities in the paragraph
58- for ( int j = 0 ; j < paragraph . ChildEntities . Count ; j ++ )
59- {
60- Entity item = paragraph . ChildEntities [ j ] ;
61- // Check if the item is the start of an editable range
62- if ( item is EditableRangeStart )
63- {
64- // Find the editable range by ID and remove it
65- EditableRange editableRange = document . EditableRanges . FindById ( ( item as EditableRangeStart ) . Id ) ;
66- if ( editableRange != null )
67- document . EditableRanges . Remove ( editableRange ) ;
68- }
69- }
70- }
52+ // Find the editable range by ID and remove it
53+ EditableRange editableRange = document . EditableRanges . FindById ( ( item as EditableRangeStart ) . Id ) ;
54+ if ( editableRange != null )
55+ document . EditableRanges . Remove ( editableRange ) ;
7156 }
7257 }
7358 }
0 commit comments