Word: SyncScrollingSideBySide and ScrollIntoView

One of the features of our Word add-in shows two document windows side by side. The user can double-click a paragraph in the left document to scroll through the related paragraph in the right document in the view. When we do this, we want to turn on synchronous scrolling again if it was turned on before double-clicking. We are doing something like this:

private void LineUpParagraphs() { // Unlock the views so we can scroll them independently. bool wasSyncEnabled = this.originalDocument.Document.Windows.SyncScrollingSideBySide; this.originalDocument.Document.Windows.SyncScrollingSideBySide = false; // Scroll corresponding original paragraph into view. this.originalDocument.Document.Windows[1].ScrollIntoView( this.CurrentOriginalParagraph.Range); // Re-enable synchronous scrolling if it was enabled before. if (wasSyncEnabled) { this.originalDocument.Document.Windows.SyncScrollingSideBySide = true; } } 

After that, the desired range is displayed in the original (to the right of our application), but as soon as you scroll through any window, the right window will return to its original position.

Things we tried that didn't work:

  • Set the SyncScrollingSideBySide property in all application windows, and not just in one of two comparison documents.
  • Toggle the property again.

We resorted to SendKeys to simulate a click on the "Synchronous Scrolling" button. (If you do not re-enable synchronization programmatically, and then press the button yourself, the right document will not return to its original position when scrolling). Actually, this is not an acceptable solution - it is inconsistent, for example, depending on whether our add-in tab is active. Sometimes it works, sometimes it switches the synchronization to extra time, which annoys the client. Is there a better way?

Note. The problem occurs when the left document is longer than the right document (the one that scrolls).

+4
source share

Source: https://habr.com/ru/post/1341321/


All Articles