Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Environment Variable

Hi All,

Is there a way from within VBA to declare an environment variable?

Or do I have to declare the kernal32 function "SetEnvironmentVariable"?

Thanks

Andy


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Environment Variable

You are correct, you do need to use an API.

You should also note that Excel seems to load all variables at startup, and
the Environ command retrieves the value from this memory store. So you need
to use the GetEnvironmentVariable to read your new value.

This should demonstrate that

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

"Andibevan" wrote in message
...
Hi All,

Is there a way from within VBA to declare an environment variable?

Or do I have to declare the kernal32 function "SetEnvironmentVariable"?

Thanks

Andy




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Environment Variable

Thanks Bob - I didn't know about using GetEnvironmentVariable.

What does the function TrimNull do? I can't work it out.

Ta

Andi



"Bob Phillips" wrote in message
...
You are correct, you do need to use an API.

You should also note that Excel seems to load all variables at startup,

and
the Environ command retrieves the value from this memory store. So you

need
to use the GetEnvironmentVariable to read your new value.

This should demonstrate that

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

"Andibevan" wrote in message
...
Hi All,

Is there a way from within VBA to declare an environment variable?

Or do I have to declare the kernal32 function "SetEnvironmentVariable"?

Thanks

Andy






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Environment Variable

Hi Andi,

It is a generic routine to tidy up strings from APIs, which use null to
designate the end of a string. So this routine just looks for the first
null, and extracts all characters to the left of that.

--
HTH

Bob Phillips

"Andibevan" wrote in message
...
Thanks Bob - I didn't know about using GetEnvironmentVariable.

What does the function TrimNull do? I can't work it out.

Ta

Andi



"Bob Phillips" wrote in message
...
You are correct, you do need to use an API.

You should also note that Excel seems to load all variables at startup,

and
the Environ command retrieves the value from this memory store. So you

need
to use the GetEnvironmentVariable to read your new value.

This should demonstrate that

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

"Andibevan" wrote in message
...
Hi All,

Is there a way from within VBA to declare an environment variable?

Or do I have to declare the kernal32 function

"SetEnvironmentVariable"?

Thanks

Andy








  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Environment Variable

I have tried the method you suggested and when the code runs as expected,
but when I type SET from the command prompt the variable is not listed.

I am trying to create a variable that would be listed when you type SET.


"Bob Phillips" wrote in message
...
Hi Andi,

It is a generic routine to tidy up strings from APIs, which use null to
designate the end of a string. So this routine just looks for the first
null, and extracts all characters to the left of that.

--
HTH

Bob Phillips

"Andibevan" wrote in message
...
Thanks Bob - I didn't know about using GetEnvironmentVariable.

What does the function TrimNull do? I can't work it out.

Ta

Andi



"Bob Phillips" wrote in message
...
You are correct, you do need to use an API.

You should also note that Excel seems to load all variables at

startup,
and
the Environ command retrieves the value from this memory store. So you

need
to use the GetEnvironmentVariable to read your new value.

This should demonstrate that

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

"Andibevan" wrote in message
...
Hi All,

Is there a way from within VBA to declare an environment variable?

Or do I have to declare the kernal32 function

"SetEnvironmentVariable"?

Thanks

Andy












  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Environment Variable

Andi,

It is only set for that session.

If you do Shell "CMD.EXE" immediately after the SetEnvironmentVariable, and
type Set you will see it

--
HTH

Bob Phillips

"Andibevan" wrote in message
...
I have tried the method you suggested and when the code runs as expected,
but when I type SET from the command prompt the variable is not listed.

I am trying to create a variable that would be listed when you type SET.


"Bob Phillips" wrote in message
...
Hi Andi,

It is a generic routine to tidy up strings from APIs, which use null to
designate the end of a string. So this routine just looks for the first
null, and extracts all characters to the left of that.

--
HTH

Bob Phillips

"Andibevan" wrote in message
...
Thanks Bob - I didn't know about using GetEnvironmentVariable.

What does the function TrimNull do? I can't work it out.

Ta

Andi



"Bob Phillips" wrote in message
...
You are correct, you do need to use an API.

You should also note that Excel seems to load all variables at

startup,
and
the Environ command retrieves the value from this memory store. So

you
need
to use the GetEnvironmentVariable to read your new value.

This should demonstrate that

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

"Andibevan" wrote in message
...
Hi All,

Is there a way from within VBA to declare an environment variable?

Or do I have to declare the kernal32 function

"SetEnvironmentVariable"?

Thanks

Andy












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
System Environment Variables bdickert Excel Discussion (Misc queries) 1 October 8th 06 08:40 PM
System Environment Variables bdickert Excel Discussion (Misc queries) 9 October 7th 06 09:34 PM
what is ERP environment Rao Ratan Singh New Users to Excel 1 April 18th 06 03:16 PM
shared environment Alex Excel Discussion (Misc queries) 1 October 5th 05 12:03 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:44 AM.

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

About Us

"It's about Microsoft Excel"