In a document with multiple paragraphs and tables, I would like to place some markers, and then find and replace them.
I saw I can do that with bookmarks:
[This is my Text
some more text
Header1 | Header2 | Header3 |
a | b | c |
d | e | f |
more text]
Here [ and ] represent the start of the bookmark. I can find the table in the bookmark, like so:
if (string.IsNullOrEmpty(bookmarkName) || _docIo.Document.Bookmarks.FindByName(bookmarkName) == null) return;
//Move to the virtual cursor before the bookmark end location of the bookmark
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(_docIo.Document);
bookmarkNavigator.MoveToBookmark(bookmarkName);
//Gets the content for the current bookmark
TextBodyPart textBodyPart = bookmarkNavigator.GetBookmarkContent();
WTable table = textBodyPart.BodyItems.OfType<WTable>().FirstOrDefault();
But my customer find it difficult to work with this, as the bookmarks are not visible by default in Word. Even when they are visible, it remains a bit tricky to keep them in the right place when the customer changes the text in the template.
So I am considering to change this to visual markers:
<mytable>This is my Text
some more text
Header1 | Header2 | Header3 |
a | b | c |
d | e | f |
more text</mytable>
Is it possible to get the TextBodyPart including the start and end marker? If I have a TextBodyPart, I can delete the markers, and find the table, and manipulate the content.
I tried using a regex with Find, but did not find a way to select this:
TextSelection selection = Document.Find(new Regex($"<mytable>.*</mytable>",RegexOptions.Singleline));
This is not working, I read somewhere that this does not find across entities.