Global variables in macro?

Hello people,

Is it possible to use global variables in my macro’s? I have written a few macro’s in the standard module, and now I would like to re-use some variables.

Instead of this:

Sub Foo
welcome = "hello"
Print welcome
End Sub

Sub Bar
welcome = "hello"
Print welcome
End Sub

I would like to be able to do something like this:

welcome = "hello"

Sub Foo
Print welcome
End Sub

Sub Bar
Print welcome
End Sub

Can it be done?

Your example suggests the use of
constants.

Actually you are right. Constants are exactly what I need!

If I put this on top:

Global Const welcome = "hi there"

Then how do I use this constant in a sub? Just like this?

Sub Foo
  Print welcome
End foo

yes we can :slight_smile:

Cool, gonna give it a try … thanks! :slight_smile:

It can be done:

global welcome 'inject to the whole namespace for whole officeruntime'
'puplic welcome 'inject into whole namespace''
'private welcome 'inject into module namespace''

Sub main
  welcome = "hallo"
End Sub


Sub Foo
  main
  Print welcome
End Sub

Sub Bar
  Print welcome
End sub

but anyway, thats bad codingstyle, if possible avoid it and pass arguments from one sub to others.

Hi

+1 for the good advise (pass arguments).

Your example suggests the use of constants. The constants are used to assign a fixed value, known when writing the program. A constant is written with the keyword Const. For example:

'Private Const welcome = "hallo"'
'Public Const welcome = "hallo"'
Global Const welcome = "hallo" 'all libraries'