PDA

View Full Version : global VBA varibles ?



sam111
12-15-2010, 09:54 AM
I have a Module in VBA. I want to have a few global varibles that I can access where every I am in the word document visual basic editor.

I declared in my module

Public strName As String

But when I try to set or get the value I get a compiler error Object required

How can I fix this? I have tried using the Global keyword what I still cann't get what I want.

Any VBA experts that can answer this.

Thanks

fumei
12-15-2010, 11:24 AM
"I have a Module in VBA." What Project is it in?

Circumstance A
NORMAL.DOT
Module: NewMacros

Public strName As String


Any OTHER document VBA Project:
Module: Module1
Sub Trythis()
strName = "yadda"
MsgBox strName
End Sub



Circumstance B
NORMAL.DOT
Module: NewMacros
Sub Trythis()
strName = "yadda"
MsgBox strName
End Sub


Any Other document:
Module: Module1

Public strName As String


Circumstance A does work.
Circumstance B does NOT work.

In BOTH cases strName is declared as Public, but in only ONE case will the TryThis procedure work.

This is Scope.

A Public variable in Normal, or any other global, is Public for all procedures, in all modules, in all Projects.

A Public variable in a standard module - NOT a userform module - document Project is Public for all procedures, all modules in THAT Project.