vidhya
vidhya

Reputation: 459

How to store the variable throughout the session?

I need to store a variable value till the document is closed.

For example: Once I open a document, I run the macro which will get variable value using "filedialog" option and that variable should be used by all other macros.

Code:

sub getvariable()

Dim docTitle As Document
Dim stylepath As String
Dim stylename As FileDialog
MsgBox ("Please choose Reference File")

Set stylename = Application.FileDialog(filedialogtype:=msoFileDialogFilePicker)
If stylename.Show Then
    stylepath = stylename.SelectedItems(1)
End If

The stylepath variable should be used by other macros as well. After I open the document I will set this variable by calling this macro function and later on that variable should be stored throughout the session and can be used by other functions as well.

Upvotes: 1

Views: 712

Answers (1)

Christina
Christina

Reputation: 1379

You can make the variable global by placing the Dim statement at the top of your module, under Option Explicit but before any subroutines. Overuse of global variables is generally considered bad form, but sometimes it's the most straightforward way to accomplish a task.

Upvotes: 2

Related Questions