Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 249
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Query window's font size is too large for window susan Excel Discussion (Misc queries) 0 September 29th 09 07:35 PM
can i size an Excel workbook window's opening size? ncjenny Excel Discussion (Misc queries) 0 November 12th 06 04:03 PM
Environment Variable Andibevan[_4_] Excel Programming 5 August 3rd 05 02:17 PM
How to make sure a Cell is within the current visible window's ran OKLover[_2_] Excel Programming 2 July 13th 05 01:38 PM
How can I invoke windows environment variable in excel Bert van den Brink Excel Programming 0 August 6th 03 08:46 PM


All times are GMT +1. The time now is 04:54 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"