Changing an Environment Variable.

  • Thread starter Thread starter Jack Gillis
  • Start date Start date
J

Jack Gillis

Is it possible to do? I know the Environ function will return the value
of one but can find nothing in Help that will allow me to change one.
No big deal, just curious.
 
With APIs

Private Declare Function SetEnvironmentVariable Lib "kernel32" _
Alias "SetEnvironmentVariableA" _
(ByVal lpName As String, _
ByVal lpValue As String) As Long


Sub xx()
SetEnvironmentVariable "Rob", "Nuzie!"
End Sub


although the limitations should be obvious, no system environment variables
without the permissions, just user environment variables.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Bob Phillips said:
With APIs

Private Declare Function SetEnvironmentVariable Lib "kernel32" _
Alias "SetEnvironmentVariableA" _
(ByVal lpName As String, _
ByVal lpValue As String) As Long


Sub xx()
SetEnvironmentVariable "Rob", "Nuzie!"
End Sub


although the limitations should be obvious, no system environment variables
without the permissions, just user environment variables.
....

The distinction doesn't apply to Windows 95/98/Me, FWIW.

The only things that would be affected by changing environment variables
from within Excel would be child processes launched from Excel via VBA's
Shell function, i.e., other processes inheriting a copy of Excel's
environment. There's no way for one process to change another process's
environment directly.
 
Harlan Grove said:
The only things that would be affected by changing environment variables
from within Excel would be child processes launched from Excel via VBA's
Shell function, i.e., other processes inheriting a copy of Excel's
environment. There's no way for one process to change another process's
environment directly.

Try this.


Private Declare Function GetEnvironmentVariable Lib "kernel32" _
Alias "GetEnvironmentVariableA" _
(ByVal lpName As String, _
ByVal lpBuffer As String, _
ByVal nSize As Long) As Long


Private Declare Function SetEnvironmentVariable Lib "kernel32" _
Alias "SetEnvironmentVariableA" _
(ByVal lpName As String, _
ByVal lpValue As String) As Long


Sub xx()
MsgBox Environ("Rob")
MsgBox GetEnvironmentVar("Rob")
SetEnvironmentVariable "Rob", "Nuzie2!"
MsgBox Environ("Rob")
MsgBox GetEnvironmentVar("Rob")

End Sub


Function GetEnvironmentVar(Name As String) As String
GetEnvironmentVar = String(255, 0)
GetEnvironmentVariable Name, GetEnvironmentVar, Len(GetEnvironmentVar)
GetEnvironmentVar = TrimNull(GetEnvironmentVar)
End Function


Private Function TrimNull(item As String)
Dim iPos As Long
iPos = InStr(item, vbNullChar)
TrimNull = IIf(iPos > 0, Left$(item, iPos - 1), item)
End Function
 
Thank you Bob and Harlan.

I think I will play around with that when I get some time.

Jack
 
Bob Phillips wrote...
Try this. ....
Sub xx()
MsgBox Environ("Rob")
MsgBox GetEnvironmentVar("Rob")
SetEnvironmentVariable "Rob", "Nuzie2!"
MsgBox Environ("Rob")
MsgBox GetEnvironmentVar("Rob")
End Sub
....

This demonstrates that Environ provides access to the *initial*
environment of Excel's process. It appears Excel loads it into an
internal table in order to provide numbered indexing as well as
standard string indexing. So, Environ isn't necessarily the Excel
process's *current* environment.

With regard to my point about environments and processes, try this.

Sub zzz()
Dim s As String
s = Environ("DEBUG")
SetEnvironmentVariable "DEBUG", IIf(s = "foo!", "bar!", "foo!")
Shell "C:\winnt\system32\cmd.exe"
End Sub

After the macro finishes, switch to the console window it spawned and
issue the command SET D. If DEBUG was originally foo!, DEBUG should
appear as bar!. Now launch another console session by holding down the
flying Windows key and pressing R, then enter CMD in the Open field. In
that console session, issue the command SET D. DEBUG should still
appear as foo!.

I should have been more explicit. Nothing is gained by setting
environment variables in Excel unless those environment variables are
intended to be used in new processes spawned from Excel. If that's
what's needed, then your SetEnvironment is what's needed.
 
Back
Top