Matteo NNZ
Matteo NNZ

Reputation: 12685

Global variable not surviving across classes?

I have one UserForm mailForm, a custom class Document and a module utilities which is declared as Option Private Module.

The following sequence might raise an error:

To sum up, the sequence is: user --> sendEmail() :: form --> generatePDF() :: class --> printPDF() :: utilities.

In the macro printPDF() of the module utilities, I might have an error raised during PDF's generation. The trigger of this error should stop the execution of the user's action to send the email.

Hence, I have declared the global variable (Dim errorWhilePDF As Boolean) and put the declaration on top of the class module. If the error occurs in the macro printPDF(), I just set errorWhilePDF = True.

My issue is that the value True of this variable set in the module utilities is not kept in the user form, which still reads it as false. If I declare the variable in the utilities module, instead, the user form sees it as Empty.

How should I declare my variable errorWhilePDF in order to have it set in the module utilities and used in the user form, knowing that I'm passing through the class Document?

Upvotes: 2

Views: 206

Answers (1)

S Meaden
S Meaden

Reputation: 8270

for truly global use Global keyword on a variable defined in a standard module not a class module. Class modules are per instance (with an exception Static class in VBA).

Upvotes: 4

Related Questions