Insert foonote right after a blank content control causes Word document to not working properly
In our VSTO Add-In, we use the following code to insert a footnote:
// Get current cursor selection
var currentSelection = _window.WordApplication.Selection;
var footnote = currentSelection .Footnotes.Add(currentSelection.range);
The code works well until we found an edge case. When the cursor is placed right after a blank content control (e.d. A rich text content control and the content controls has to be blank, no such issue if there are some content in the content control), then the code runs fail and throws following exception and the Word document would stop working, cannot input and scroll (even the exception was caught):
2022-09-19 15:47:15,029 [VSTA_Main] ERROR: System.Runtime.InteropServices.COMException (0x800A11FD): This
method or property is not available because the current selection is outside of a block-level XML element.
It seems like the place is an invalid place to insert footnote, since I saw the Insert Footnote
button in the References
tab was grayed out.
My question is, for the issue I met, is there any way in the Interop that I can use to determine if the cursor's position/selection is a valid place to insert the footnote? (How to check if a cursor is outside of a block-level XML element?)
If some C# code sample can be provided, that would be helpful.