Forum Discussion
OneNote Interop API targets wrong window instance
C# with .NET Framework 4.8, Microsoft OneNote
Open up two OneNote windows viewing the same page. From the second window, invoke an add-in command that uses the managed Interop API.
Any queries or updates made by the API will target the first OneNote window instance, even while the second window instance is active and has focus.
However, if the content of the page is manually modified from the second [active] window instance, then and only then will the API call target and update the page in the second window.
Expectation is that the updated content will be visible immediately in the active OneNote window instance
4 Replies
- saleha15Brass Contributor
This sounds like a limitation of how the OneNote Interop API resolves the application/page context rather than a normal focus issue. The managed Interop API may not necessarily bind its operations to the currently focused OneNote window, especially when the same page is open in multiple windows
- stemarcohTin Contributor
Yes, exactly. Thanks for this. I have at least a few users that normally open the same page in two, even three windows while working on large documents. Being able to affect change when and where they are focusing their attention is important to the intuitive flow of the add-in.
You are invoking the add-in from the second OneNote window, but the application-level Interop calls behave as though the first window owns the current page until the second window is edited. Microsoft exposes open windows through Application.Windows: CurrentWindow identifies the active Window, and each Window provides Active and CurrentPageId. Capture Windows.CurrentWindow at command start, verify that Active is true, and store its CurrentPageId before making a content call. Enumerate the Windows collection to confirm which instance OneNote reports as active. UpdatePageContent targets content by page ID; it does not target a UI window. When two windows show the same page, the API offers no separate window-specific update target. After the update, reactivate the captured Window and verify its CurrentPageId. If OneNote still refreshes only the first window, provide a minimal two-window reproduction and build number to Microsoft. The documentation does not guarantee that UpdatePageContent refreshes the invoking window.
- stemarcohTin Contributor
This has happened in all version of OneNote, all build numbers that I've tried, since early releases of OneNote 2021; it is a systemic issue in the Interop API. Let me give you a more clear example.
- Open page (A) in window (1) and set the cursor on paragraph (a)
- Open page (A) in window (2) and set the cursor on paragraph (b) -- window (2) is active
- GetPageContent(CurrentPageId) returns XML showing that the cursor is on paragraph (a)
- Any add-in logic that depends on the text cursor location (selected=all) or selection range will update paragraph (a), not paragraph (b).
This is unexpected from the end user's perspective, and from my perspective as a developer.
You can reverse that, continuing from step 4 above:
- Make any change in window (2) and set the cursor on paragraph (b)
- Click on window (1) to make it active and set the cursor on paragraph (a)
- GetPageContent will return XML showing the cursor on paragraph (b)
Again, wrong context. This is a focus problem that add-ins should not be responsible for solving in my opinion.