Good afternoon,
I have attached a Word document which should allow you to recreate the issue I am experiencing.
I have written code to build a list of all Bookmarks within a Word document and to then cycle through these and retrieve the text within the Range of the Bookmark. As you can see below these Bookmarks span text content, appear in Table Cells and, in many cases, a single Cell contains 2 Bookmarks:
The problem I'm experiencing is that, taking the "Unit Shipping Weight" [value] cell as an example, the text retrieved for each Bookmark is "2413 kg" whereas one of the two Bookmarks should return "2413" and the other should return "kg".
Could you review my code and suggest a modification that would allow just the content within a specific Bookmark to be returned for that Bookmark please?
public List<BookmarksAndValues> GetWordDocumentBookmarksAndValues(ref WordDocument wordDoc)
{
List<BookmarksAndValues> bookmarksAndValues = new List<BookmarksAndValues>();
Bookmark bookmark;
BookmarksNavigator bkNavigator = new BookmarksNavigator(wordDoc);
IEnumerable<string> bookmarkNamesList = GetWordDocumentBookmarkNamesList(ref wordDoc);
foreach (string sBookmarkName in bookmarkNamesList)
{
bookmark = wordDoc.Bookmarks.FindByName(sBookmarkName);
if (bookmark != null)
{
try
{
bkNavigator.MoveToBookmark(sBookmarkName, true, false);
try
{
TextBodyPart bookmarkContent = bkNavigator.GetBookmarkContent();
string bookmarkText = ((Syncfusion.DocIO.DLS.WParagraph)bookmarkContent.BodyItems.FirstItem).Text;
bookmarksAndValues.Add(new BookmarksAndValues(sBookmarkName, bookmarkText));
}
catch
{
//Continue to next Bookmark.
}
}
catch (Exception ex)
{
throw new Exception("The bookmark " + sBookmarkName + " has an error.");
}
}
}
return bookmarksAndValues;
}
Thank you,
Jason.
Attachment:
Sample_7235fd3b.zip