@@ -22,7 +22,6 @@ static void Main(string[] args)
2222 #region Insert bookmarks at placeholders
2323 //Unique ID for each bookmark.
2424 int bkmkId = 1 ;
25- string bookmarkName = string . Empty ;
2625 //Collection to hold the inserted bookmarks.
2726 List < string > bookmarks = new List < string > ( ) ;
2827 //Iterate each text selection.
@@ -34,7 +33,7 @@ static void Main(string[] args)
3433 //Get the index of the placeholder text.
3534 WParagraph startParagraph = textRange . OwnerParagraph ;
3635 int index = startParagraph . ChildEntities . IndexOf ( textRange ) ;
37- bookmarkName = "Bookmark_" + bkmkId ;
36+ string bookmarkName = "Bookmark_" + bkmkId ;
3837 //Add new bookmark to bookmarks collection.
3938 bookmarks . Add ( bookmarkName ) ;
4039 //Create bookmark start.
@@ -68,17 +67,18 @@ static void Main(string[] args)
6867 int fileIndex = 1 ;
6968 foreach ( string bookmark in bookmarks )
7069 {
71- MemoryStream memoryStream = new MemoryStream ( ) ;
7270 //Move the virtual cursor to the location before the end of the bookmark.
7371 bookmarksNavigator . MoveToBookmark ( bookmark ) ;
7472 //Get the bookmark content as WordDocumentPart.
7573 WordDocumentPart wordDocumentPart = bookmarksNavigator . GetContent ( ) ;
7674 //Save the WordDocumentPart as separate Word document.
77- WordDocument newDocument = wordDocumentPart . GetAsWordDocument ( ) ;
78- //Save the Word document to file stream.
79- using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"../../../Placeholder_" + fileIndex + ".docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
75+ using ( WordDocument newDocument = wordDocumentPart . GetAsWordDocument ( ) )
8076 {
81- newDocument . Save ( outputFileStream , FormatType . Docx ) ;
77+ //Save the Word document to file stream.
78+ using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"../../../Placeholder_" + fileIndex + ".docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
79+ {
80+ newDocument . Save ( outputFileStream , FormatType . Docx ) ;
81+ }
8282 }
8383 fileIndex ++ ;
8484 }
0 commit comments