Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can you set a Window's Environment variable in VB
Is there any way to set a Window's Environment variable from within Visual
Basic Code. I know I can read one via the VB Environ. thanks, ken |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can you set a Window's Environment variable in VB
http://www.microsoft.com/windows2000...ing/setx-o.asp
-- Regards, Dave Patrick ....Please no email replies - reply in newsgroup. Microsoft Certified Professional Microsoft MVP [Windows] http://www.microsoft.com/protect "Ken Soenen" wrote: | Is there any way to set a Window's Environment variable from within Visual | Basic Code. I know I can read one via the VB Environ. | | thanks, | ken | | |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can you set a Window's Environment variable in VB
From a previous post.
That is exactly what we use it for, dev and prod. What problem exactly are you getting with the SetEnvironmentVariable API? Is it that trying to read it with Environ in the same NT/XP session does return a value? If that is the case, I think this is because Excel seems to load the environment variables at start-up, and Environ gets the values locally. So any changes that you make are not reflected in Environ. And as SetEnvironmentVariable only sets the variable for the current session, this stymies you. You can get around it though by using GetEnvironmentVariable to read it as this will read any variables in the current session. This code snippet should demonstrate it, GetEnvironmentVariable returns the newly set value, Environ doesn't Option Explicit 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() SetEnvironmentVariable "Rob", "Nuzie!" 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 -- HTH Bob Phillips (remove nothere from email address if mailing direct) "Ken Soenen" wrote in message ... Is there any way to set a Window's Environment variable from within Visual Basic Code. I know I can read one via the VB Environ. thanks, ken |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can you set a Window's Environment variable in VB
Thanks Bob and Dave for the responses.
I downloaded SETX as suggested by Dave and it will set any EXISTING variable. The SetEnvironmentVariable function suggested by Bob does the same. The GetEnvironmentVariable function appears to get the variable if it is a NEW variable(set by SetEnvironmentVariable) but not if it is an EXISTING variable. Again, thanks for your responses. I'm a lot closer now than I was before. ken "Bob Phillips" wrote in message ... From a previous post. That is exactly what we use it for, dev and prod. What problem exactly are you getting with the SetEnvironmentVariable API? Is it that trying to read it with Environ in the same NT/XP session does return a value? If that is the case, I think this is because Excel seems to load the environment variables at start-up, and Environ gets the values locally. So any changes that you make are not reflected in Environ. And as SetEnvironmentVariable only sets the variable for the current session, this stymies you. You can get around it though by using GetEnvironmentVariable to read it as this will read any variables in the current session. This code snippet should demonstrate it, GetEnvironmentVariable returns the newly set value, Environ doesn't Option Explicit 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() SetEnvironmentVariable "Rob", "Nuzie!" 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 -- HTH Bob Phillips (remove nothere from email address if mailing direct) "Ken Soenen" wrote in message ... Is there any way to set a Window's Environment variable from within Visual Basic Code. I know I can read one via the VB Environ. thanks, ken |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
MS Query window's font size is too large for window | Excel Discussion (Misc queries) | |||
can i size an Excel workbook window's opening size? | Excel Discussion (Misc queries) | |||
Environment Variable | Excel Programming | |||
How to make sure a Cell is within the current visible window's ran | Excel Programming | |||
How can I invoke windows environment variable in excel | Excel Programming |