Insert foonote right after a blank content control causes Word document to not working properly

Chris Yang 1 Reputation point
2022-09-19T20:34:23.183+00:00

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.

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,869 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.