Forum Discussion
OneNote Interop API targets wrong window instance
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.
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.