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