none
Same values of variables in word 2010 Visual Studio add-in are being shared across multiple document RRS feed

  • Question

  • Hi,

    I am developing an Office Word 2010 Add-In in Visual Studio using c#. In add-in code I have some class level private variables declared.

    When a word document is opened then some of these variables are loaded with dynamic values and some are loaded with values in various events like DocumentOpen etc. It's fine till here.

    Now I keep the first document opened and open another word document. When this second document is opened while debugging before load event I can see the variables are already loaded with the first document's values.

    So if first document 'Doc1' has a variable named 'docName' which I assign with value "Doc1" when the document is opened. Now when I open another word document 'Doc2' (first document 'Doc1' is still opened, not closed) I see the variable 'docName' already set with value 'Doc1'.

    This is not only happening when the Documents are opened but in every events all variables' values are shared across multiple documents.

    I do not want to create variable arrays as there are many variables and will keep adding more as functionality of add-in is extended.

    Any kind of help is highly appreciated.

    Thanks in advance.

    Regards,

    Syed Abbas


    Regards, Syed Gulam Abbas

    • Moved by Tony Chen CHN Thursday, January 16, 2014 3:17 AM Word Dev
    Wednesday, January 15, 2014 2:08 PM

All replies

  • Hi Syed,

    I'd like to move your question to the MSDN Visual Studio tools for Office forum as this is more likely something that can be answered by people who specialize in configuration. Thanks your understanding.

    Cheers,
    Tony Chen
    Forum Support
    ________________________________________
    Come back and mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback on our support, please contact tnmff@microsoft.com.

    Thursday, January 16, 2014 3:16 AM
  • Hi Syed,

    According to your description, you don’t want the variable to be shared across multiple documents.

    The reason why the variable shared across multiple documents is that the add-in will be loaded when the Word application progress started. When you open the multiple word document, it won’t start another Word progress.

    We can get the active document name from Application.ActiveDocument.Name and you can get all opened document by looping the _Application.Documents.

    If you do need to declare variable for every document, I suggest you using generic collections. You can get more detail about generic collection from link below:
    System.Collections.Generic

    Hope it can help you.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 16, 2014 11:05 AM
    Moderator
  • Hi Fei,

    Thanks for the timely response and useful insight into how word document is loaded.

    I am really not in favour of using arrays or collections as it will complicate an already complicated add-in.

    But If end up using the arrays or something I will consider Generic Collections.

    Thanks once again.


    Regards, Syed Gulam Abbas

    Friday, January 17, 2014 2:10 PM