none
Word VBA: Session Variables RRS feed

  • Question

  • Thanks for taking the time to read my question.

    Does Word 2010+ have a way to store a variable as long as Word is open? Is this just a Global variable or is there a better way?

    I know Access has a new way called "TempVars" which are more reliable than Global Variables.

    My idea is to read in a few settings from a txt file stored on the network. This way I don't have to update everyone's macro, I can just edit the one text file.

    Thanks,
    Brad

    Friday, March 23, 2018 3:12 PM

Answers

  • Hi mbrad,

    You had asked,"Does Word 2010+ have a way to store a variable as long as Word is open? Is this just a Global variable or is there a better way?"

    Word lets you to store a document variable. Which is available as long as Word document is open.

    Example Code:

    Sub demo()
    
    ActiveDocument.Variables.Add Name:="Temp2", Value:="10"
    MsgBox ActiveDocument.Variables("Temp2").Value
    End Sub

    Reference:

    Variable.Value Property (Word)

    So If you just want to store variable until specific document is open then Document.Variable property can work for you otherwise you can use global variables.

    Note that, If you close this document and reopen it then also this value of Document variable will be preserved.

    It means that once you set this value, You can access it any time. You not need to set it again.

    Regards

    Deepak


    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. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mbrad Monday, March 26, 2018 1:38 PM
    Monday, March 26, 2018 5:55 AM
    Moderator

All replies

  • Hi mbrad,

    You had asked,"Does Word 2010+ have a way to store a variable as long as Word is open? Is this just a Global variable or is there a better way?"

    Word lets you to store a document variable. Which is available as long as Word document is open.

    Example Code:

    Sub demo()
    
    ActiveDocument.Variables.Add Name:="Temp2", Value:="10"
    MsgBox ActiveDocument.Variables("Temp2").Value
    End Sub

    Reference:

    Variable.Value Property (Word)

    So If you just want to store variable until specific document is open then Document.Variable property can work for you otherwise you can use global variables.

    Note that, If you close this document and reopen it then also this value of Document variable will be preserved.

    It means that once you set this value, You can access it any time. You not need to set it again.

    Regards

    Deepak


    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. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mbrad Monday, March 26, 2018 1:38 PM
    Monday, March 26, 2018 5:55 AM
    Moderator
  • Thank you Deepak for your reply. 

    Worked great.

    Have a super day.

    Brd

    Monday, March 26, 2018 1:39 PM