ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How do I set an environment variable to a string (https://www.excelbanter.com/excel-programming/415570-how-do-i-set-environment-variable-string.html)

ChristopherL

How do I set an environment variable to a string
 
My question is on the subject line.

Thank You
Chris

Jim Thomlinson

How do I set an environment variable to a string
 
We need more info than that. What exactly are you wanting to do...

dim str as string

str = environ("userName")
msgbox str

--
HTH...

Jim Thomlinson


"ChristopherL" wrote:

My question is on the subject line.

Thank You
Chris


Bob Bridges

How do I set an environment variable to a string
 
Says here there's an Environ function you can use, eg vx = Environ("PATH").

--- "ChristopherL" wrote:
How do I set an environment variable to a string


ChristopherL

How do I set an environment variable to a string
 
On Aug 12, 6:10*pm, Bob Bridges
wrote:
Says here there's an Environ function you can use, eg vx = Environ("PATH").



--- "ChristopherL" wrote:
How do I set an environment variable to a string- Hide quoted text -


- Show quoted text -


Sorry, I need to clarify my question. I did not want to determine the
contents of an environment variable! What I want to do is be able to
go out to the "command prompt" and set an arbitrary environment
variable.

I tried the following, but VBA tells me the path to the command was
not found:

dim ev_result as string

en_result = shell ("""set chris=1 2 3""")

The 3 sets of double quotes is to try to simulate doing the following
in a command prompt window: set chris="1 2 3".

Again, I want to set an environment variable within VBA.

Thank you,
Chris

ChristopherL

How do I set an environment variable to a string
 
On Aug 13, 12:12*am, ChristopherL wrote:
On Aug 12, 6:10*pm, Bob Bridges
wrote:

Says here there's an Environ function you can use, eg vx = Environ("PATH").


--- "ChristopherL" wrote:
How do I set an environment variable to a string- Hide quoted text -


- Show quoted text -


Sorry, I need to clarify my question. I did not want to determine the
contents of an environment variable! What I want to do is be able to
go out to the "command prompt" and set an arbitrary environment
variable.

I tried the following, but VBA tells me the path to the command was
not found:

dim ev_result as string

en_result = shell ("""set chris=1 2 3""")

The 3 sets of double quotes is to try to simulate doing the following
in a command prompt window: set chris="1 2 3".

Again, I want to set an environment variable within VBA.


Sorry again, I think I have a partial solution, if not a complete
solution! I'll try it tomorrow. Please critique me

dim ev_result as string

ev_result = shell ("\cmd ""set chris=1 2 3""")

en_result = Environ ("chris")
'en_result should be "1 2 3"

Thank you everyone in VBA land,
Chris


Bob Bridges

How do I set an environment variable to a string
 
Ah, right you are; I misread the question, reversing the subject and
predicate. Ok, I know I've run across this somewhere; I think it's a
property of one of those COM objects like wscript or WSH or something.

....Ah, here it is. Well, maybe it is. Try this:

Set ows = WScript.CreateObject("WScript.Shell")
Set oEnv = ows.Environment("SYSTEM")
oEnv("PATH") = oEnv("PATH") & ";c:\Utils"

I got this sequence from
http://msdn.microsoft.com/en-us/libr...a0(VS.85).aspx, except it
mentions only display without saying whether you can also change the
environment variable. Give it a try; maybe it'll work.

--- "ChristopherL" wrote:
Sorry, I need to clarify my question. I did not want to determine the
contents of an environment variable! What I want to do is be able to
go out to the "command prompt" and set an arbitrary environment
variable.

I tried the following, but VBA tells me the path to the command was
not found:

dim ev_result as string
en_result = shell ("""set chris=1 2 3""")

The 3 sets of double quotes is to try to simulate doing the following
in a command prompt window: set chris="1 2 3".


On Aug 12, 6:10 pm, Bob Bridges
wrote:
Says here there's an Environ function you can use, eg vx = Environ("PATH").


--- "ChristopherL" wrote:
How do I set an environment variable to a string- Hide quoted text -


Dave Peterson

How do I set an environment variable to a string
 
The environment variable would be alive as long as the Shell is alive--but it's
limited to that shell and its children.

So if you need to run a .bat file that needs an environment variable, you could
shell to a different .bat file that sets the environment variable and then runs
the .bat file that you need to run.

But if you want to change the environment variable globally, it's a different
story.

Win95 came with command called WinSet that did this. But I don't see it in
WinXP (at least in a normal install).

But you could search google looking for alternatives.

I've never used this, but it looks interesting:
http://www.codeguru.com/cpp/w-p/win3...le.php/c10849/

ChristopherL wrote:

I modified test5 to make the shell command wait till the command
completes, but was rewarded with the same results, nothing printed!
Chris

(5) The following did not work either:

Sub test5()

Dim ev_result As String

ev_result = Shell("cmd /c set chris=""1 2 3""")
ev_result = Environ("chris")

Debug.Print ev_result '---Prints Nothing!

End Sub


--

Dave Peterson

ChristopherL

How do I set an environment variable to a string
 
On Aug 13, 7:31*am, Dave Peterson wrote:
The environment variable would be alive as long as the Shell is alive--but it's
limited to that shell and its children.

So if you need to run a .bat file that needs an environment variable, you could
shell to a different .bat file that sets the environment variable and then runs
the .bat file that you need to run.

But if you want to change the environment variable globally, it's a different
story. *

Win95 came with command called WinSet that did this. *But I don't see it in
WinXP (at least in a normal install).

But you could search google looking for alternatives.

I've never used this, but it looks interesting:http://www.codeguru.com/cpp/w-p/win3...le.php/c10849/

....

Dave Peterson- Hide quoted text -

- Show quoted text -


Dave, I tried the following, but in the command prompt window it
said:

'%chris%' is not recognized as an internal or external
command, operable program or batch file.
Chris

Sub test6()

Dim count As Long
Dim ev_result As String

Row = 30

count = 0

ev_result = Wait_("cmd /k set chris=""1 2 3"" & %chris%", "", False,
True)
ev_result = Environ("chris")

Debug.Print "--- " & ev_result & " <---"

Debug.Print "end"
End Sub

ChristopherL

How do I set an environment variable to a string
 
Dave, in the interest of saving time, here is a basic test. It gives
the same results.The command prompt window said:

'%chris%' is not recognized as an internal or external
command, operable program or batch file.

Chris


Sub test7()

Dim count As Long
Dim ev_result As String

Row = 30

count = 0

ev_result = Shell("cmd /k set chris=""1 2 3"" & %chris%")
ev_result = Environ("chris")

Debug.Print "--- " & ev_result & " <---"

Debug.Print "end"
End Sub


Dave Peterson

How do I set an environment variable to a string
 
I don't understand what this:
ev_result = Wait_("cmd /k set chris=""1 2 3"" & %chris%", "", False, True)
is doing.

And I don't understand what you're really trying to do.

Are you trying to create an environment variable that's available for just the
duration of that Shell command--and only available to the that Shell?

Or are you trying to create an environment variable that can be seen by
anything?



ChristopherL wrote:

On Aug 13, 7:31 am, Dave Peterson wrote:
The environment variable would be alive as long as the Shell is alive--but it's
limited to that shell and its children.

So if you need to run a .bat file that needs an environment variable, you could
shell to a different .bat file that sets the environment variable and then runs
the .bat file that you need to run.

But if you want to change the environment variable globally, it's a different
story.

Win95 came with command called WinSet that did this. But I don't see it in
WinXP (at least in a normal install).

But you could search google looking for alternatives.

I've never used this, but it looks interesting:http://www.codeguru.com/cpp/w-p/win3...le.php/c10849/

...

Dave Peterson- Hide quoted text -

- Show quoted text -


Dave, I tried the following, but in the command prompt window it
said:

'%chris%' is not recognized as an internal or external
command, operable program or batch file.
Chris

Sub test6()

Dim count As Long
Dim ev_result As String

Row = 30

count = 0

ev_result = Wait_("cmd /k set chris=""1 2 3"" & %chris%", "", False,
True)
ev_result = Environ("chris")

Debug.Print "--- " & ev_result & " <---"

Debug.Print "end"
End Sub


--

Dave Peterson

ChristopherL

How do I set an environment variable to a string
 
On Aug 13, 7:47*am, Dave Peterson wrote:
I don't understand what this:
ev_result = Wait_("cmd /k set chris=""1 2 3"" & %chris%", "", False, True)
is doing.

And I don't understand what you're really trying to do.

Are you trying to create an environment variable that's available for just the
duration of that Shell command--and only available to the that Shell?

Or are you trying to create an environment variable that can be seen by
anything?





ChristopherL wrote:

On Aug 13, 7:31 am, Dave Peterson wrote:
The environment variable would be alive as long as the Shell is alive--but it's
limited to that shell and its children.


So if you need to run a .bat file that needs an environment variable, you could
shell to a different .bat file that sets the environment variable and then runs
the .bat file that you need to run.


But if you want to change the environment variable globally, it's a different
story.


Win95 came with command called WinSet that did this. *But I don't see it in
WinXP (at least in a normal install).


But you could search google looking for alternatives.


I've never used this, but it looks interesting:http://www.codeguru.com/cpp/w-p/win3...le.php/c10849/


...


Dave Peterson- Hide quoted text -


- Show quoted text -


Dave, I tried the following, but in the command prompt window it
said:


* * * * * * * * '%chris%' is not recognized as an internal or external
command, operable program or batch file.
Chris


Sub test6()


* Dim count As Long
* Dim ev_result As String


* Row = 30


* count = 0


* ev_result = Wait_("cmd /k set chris=""1 2 3"" & %chris%", "", False,
True)
* ev_result = Environ("chris")


* Debug.Print "--- " & ev_result & " <---"


* Debug.Print "end"
End Sub


--

Dave Peterson- Hide quoted text -

- Show quoted text -



For every one who has not formally tuned in, I've added a few comments
to the test.
After executing the code, the command prompt windows stays around and
says:

'%chris%' is not recognized as an internal or external command,
operable program or batch file.

Chris

Sub test7()

Dim Task_ID As Variant
Dim ev_result As String

'Create shell execute a set command, and then see if can use the
contents of the variable
Task_ID = Shell("cmd /k set chris=""1 2 3"" & %chris%") '/k means
stay around, & means do 2nd cmd

'Give it another try, see if environment variable still exists
ev_result = Environ("chris")

Debug.Print "--- " & ev_result & " <---"

End Sub

ChristopherL

How do I set an environment variable to a string
 
On Aug 13, 7:47*am, Dave Peterson wrote:

....

And I don't understand what you're really trying to do.

Are you trying to create an environment variable that's available for just the
duration of that Shell command--and only available to the that Shell?

Or are you trying to create an environment variable that can be seen by
anything?


I want to set an environment variable and some how later use it's
contents in a program.

Chris



ChristopherL

How do I set an environment variable to a string
 
On Aug 13, 8:08*am, ChristopherL wrote:
On Aug 13, 7:47*am, Dave Peterson wrote:
And I don't understand what you're really trying to do.


Sub test8()

Dim Task_ID As Variant
Dim ev_result As String

'Create shell execute a set command, and then see if can use the
contents of the variable
Task_ID = Shell("cmd /k set chris=""1 2 3"" & %chris%")

'Give it another try, see if environment variable still exists
ev_result = Environ("chris")

Debug.Print "--- " & ev_result & " <---"

End Sub

After executing the code, the command prompt windows stays around and
says:

'%chris%' is not recognized as an internal or external command,
operable program or batch file.

But, I would have gotten "closer" to solving my problem if the error
had said:

'1 2 3' is not recognized as an internal or external command,
operable program or batch file.

This is because I was able to set an environment variable through VBA.

Sub test8()

Dim Task_ID As Variant
Dim ev_result As String

'Create shell execute a set command, and then see if can use the
'contents of the variable
Task_ID = Shell("cmd /k set chris=""1 2 3"" & %chris%")

'The /k means keep window around, an dthe & means do 2nd command in
same window

'Now, give it another try to see if environment variable still
exists
ev_result = Environ("chris")

Debug.Print "--- " & ev_result & " <---"

End Sub

That's all I want to do.
Chris

ChristopherL

How do I set an environment variable to a string
 
On Aug 13, 7:31*am, Dave Peterson wrote:

But if you want to change the environment variable globally, it's a different
story. *


Dave and every one, I have to be able to change the environment
variable globally.

Chris



Dave Peterson

How do I set an environment variable to a string
 
Did you try the program at that url I posted?

ChristopherL wrote:

On Aug 13, 7:31 am, Dave Peterson wrote:

But if you want to change the environment variable globally, it's a different
story.


Dave and every one, I have to be able to change the environment
variable globally.

Chris


--

Dave Peterson


All times are GMT +1. The time now is 03:50 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com