Ask Your Question
0

Global variables in macro?

asked 2015-03-10 01:57:04 +0200

LibreGuy gravatar image

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?

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
0

answered 2015-03-10 09:43:03 +0200

pierre-yves samyn gravatar image

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'
edit flag offensive delete link more
0

answered 2015-03-10 12:55:24 +0200

LibreGuy gravatar image

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
edit flag offensive delete link more

Comments

yes we can :)

pierre-yves samyn gravatar imagepierre-yves samyn ( 2015-03-10 14:10:45 +0200 )edit

Cool, gonna give it a try ... thanks! :)

LibreGuy gravatar imageLibreGuy ( 2015-03-10 20:48:11 +0200 )edit
0

answered 2015-03-10 08:20:24 +0200

karolus gravatar image

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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-03-10 01:57:04 +0200

Seen: 2,507 times

Last updated: Mar 10 '15