none
problems with WindowSelectionChange and updating ribbon on change of selection in table RRS feed

  • Question

  • Hi,

    in my solution for Word 2007-2016 I have documents based on a template that shows a table with several Content Controls and I want my ribbon to change depending on the position of the cursor.

    I'd like to update the ribbon if

    1. the selection goes from out of the table to inside of the table and viceversa.
    2. change from one row to another one.

    I don't want to update if:

    1. the selection goes from one point outside the table to another point outside the table
    2. the selection remains in the same row
    3. the code its performg its one operations on the rows.

    I succeded to achieve all that creating a new application class (clsEventApp) with new:

    1. Public WithEvents App As Word.Application
    2. Private Sub App_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
    3. Private Sub App_WindowSelectionChange(ByVal SEL As Selection)

    and inside Document_New and Document_Open I insert:

       Set gMyApp = New clsEventApp
       Set gMyApp.App = Word.Application

    WindowActivate only replicates WindowSelectionChange.

    But I'm experincing strange behaviors when multiple documents, based on the same template, are opened.

    • when the first document is created or opened WindowActivate is not triggered, when already one is opened is triggered. Why?
    • when I open also another document based on a similar but different application class both procedure inside WindowSelectionChange are performed. Why?
    • I was keeping track of the position of the cursor writing it inside a Document Variable and the reading that value to update the ribbon to avoid confusion between several document based on the same template. For a while seemed to work, but then (I don't know what I did) and now the updating occours only in the last doc I'm working with. When I close the last document the prevoius document's ribbon does not update anymore.

    In few words, I want that each document ribbon update correctly when is activated, and  each one need to keep track of the position of the cursor inside it.

    Do you have suggestions about that scenario?

    Do you think is it better that I keep track of the cursor inside each document in a Doc Variable or in a special object that I will create on each change of selection? Keep in mind that alredy I create a similar object to retrieve all the data needed to update the ribbon (the various enabled, labels, supertips, etc.). If I create those objects I need to put inside it the name of the document for wich they work.

    I' sorry maybe I didn't explain my problem precisely.

    Thanks, Lauro

    • Edited by Lauro2 Thursday, November 1, 2018 10:39 AM
    Thursday, November 1, 2018 10:37 AM

All replies

  • Hi,

    I think I solved one of my problems.

    WindowSelectionChange event is called at the application level.

    So If I want that the code in the WindowSelectionChange occours only in document based on a specific template, I have to inserti it a conditional: if ActiveDocument.AttachedTemplate = ThisDocument.

    Lauro

    Thursday, November 1, 2018 12:56 PM
  • Hi Lauro,

    Thanks for your asking. Did you resolve your issue? You can mark your solution as answer and close the post.

    Thank you for understanding. If you have any question, or update, please feel free to let us know.

    I wish you a happy life!

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Friday, November 2, 2018 1:58 AM
    Moderator