PDA

View Full Version : Use variable in other procedures



saban
06-26-2006, 08:17 AM
sFileRtfToSave = "R:\Trans\" + sFileRtf
ActiveDocument.SaveAs sFileRtfToSave, FileFormat:=wdFormatRTF

This saves the file and puts its name into variable but when procedure ends and macro shifts to another procedure this variable is empty how can I use variable "sFileRtfToSave" in another procedure

Thnx

fumei
06-26-2006, 08:33 AM
Actually you are incorrect. When your code runs another procedure the variable is NOT empty. It is out of scope, and does not even exist. Variables declared within a procedure only exist within that procedure. When the procedure terminates, all memory addresses assigned to procedure level variables are released.

Declare it as a public variable. You need to look up Scope in Help. Scope is a very important subject. Do not fall for going too much the other way - making all variables Public. Use variables with the proper scope for what they need to do.

Killian
06-26-2006, 08:35 AM
Scope. Like this. (http://www.vbaexpress.com/forum/showthread.php?t=7762)

saban
06-26-2006, 09:18 AM
Aha or I declare variable public, or rewrite it for each procedure right?
Instead of making it public I can write it in each procedure? Will this give me the same effect as declaring it public

thnx

mdmackillop
06-26-2006, 10:15 AM
Not necessarily.
MyDoc = ActiveDocument.Name can set the value of a Public Variable. MyDoc, used in other routines, will refer to that original value. If that document is closed by the code, how would you delare it in another routine, without hard coding it?
Regards
MD

fumei
06-26-2006, 10:49 AM
Instead of making it public I can write it in each procedure? Will this give me the same effect as declaring it publicWell, yes of course you can write it each procedure. But No, No, No - it will not have the same effect as declaring it as Public. The value would never be passed from procedure to procedure.

As stated, a variable declared in a procedure only exists in that procedure. You can have 10 procedures with each one having a variable named myVar. Each myVar is absolutely independent of the others. They ONLY hold value within that procedure.

Take a look at this.Sub One()
Dim sVar As String
sVar = "Hello"
Call Two
MsgBox sVar
End Sub

Sub Two()
Dim sVar As String
sVar = "Goodbye"
End SubTwo variables, both named sVar. What do you think the message will display? Let's run through.

In Sub One, sVar becomes "Hello", then control is passed to Sub Two. Yes, THAT sVar becomes "Goodbye". When Sub Two is terminated...so is the sVar in the procedure. Control is passed back to Sub One...and sVar is "Hello". It can NOT be changed by anything that happens in Sub Two, because Sub Two has its own scope.
Public sVar As String
Sub One()
sVar = "Hello"
Msgbox sVar ' displays "Hello"
Call Two
MsgBox sVar ' displays "Goodbye"
End Sub

Sub Two()
sVar = "Goodbye"
End SubMessage box will display "Hello" then display "Goodbye" The scope of the variable is global, it is set for "Hello" in Sub One, then set for "Goodbye" in Sub Two.

You MUST look at Scope!

saban
06-26-2006, 11:03 AM
thnx man I really apreciate this
stay cool