PC Review


Reply
Thread Tools Rate Thread

How do I set an environment variable to a string

 
 
ChristopherL
Guest
Posts: n/a
 
      12th Aug 2008
My question is on the subject line.

Thank You
Chris
 
Reply With Quote
 
 
 
 
Jim Thomlinson
Guest
Posts: n/a
 
      12th Aug 2008
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
>

 
Reply With Quote
 
Bob Bridges
Guest
Posts: n/a
 
      12th Aug 2008
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

 
Reply With Quote
 
ChristopherL
Guest
Posts: n/a
 
      13th Aug 2008
On Aug 12, 6:10*pm, Bob Bridges <BobBrid...@discussions.microsoft.com>
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
 
Reply With Quote
 
ChristopherL
Guest
Posts: n/a
 
      13th Aug 2008
On Aug 13, 12:12*am, ChristopherL <clusard...@aol.com> wrote:
> On Aug 12, 6:10*pm, Bob Bridges <BobBrid...@discussions.microsoft.com>
> 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

 
Reply With Quote
 
ChristopherL
Guest
Posts: n/a
 
      13th Aug 2008
On Aug 12, 9:10*pm, Bob Bridges <BobBrid...@discussions.microsoft.com>
wrote:
> 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 fromhttp://msdn.microsoft.com/en-us/library/6s7w15a0(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 <BobBrid...@discussions.microsoft.com>
> > 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 -- Hide quoted text -

>
> - Show quoted text -


I tried five tests, below, but all failed, what to do? Chris

(1) On identifier WScript, the first test told me the following:
Variable not defined

Sub test1()

Dim ows As Variant
Dim oEnv As Variant

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

(2) With test 2 on identifier WScript, it told me the following:
Variable not defined

Sub test2()

Dim WshShell As Variant
Dim WshSysEnv As Variant

Debug.Print "hello"
Debug.Print Environ("PATH")

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PATH")
WshSysEnv("PATH") = WshSysEnv("PATH") & ";c:\Utils"

Debug.Print Environ("PATH")
Debug.Print "goodbye"
End Sub

(3)The following ran but printed only "hello" and "goodbye".

Sub test3()

Dim WshShell As Variant
Dim WshSysEnv As Variant

Debug.Print "hello"
Debug.Print Environ("VOLATILE")

Set WshShell = CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("VOLATILE")
WshSysEnv("VOLATILE") = WshSysEnv("VOLATILE") & ";c:\Utils"

Debug.Print Environ("VOLATILE")
Debug.Print "goodbye"
End Sub

(4)The following gave me the error: Run-time error '5': Invalid
procedure call or argument on the line of code

Set WshSysEnv = WshShell.Environment("PATH")

Sub test4()

Dim WshShell As Variant
Dim WshSysEnv As Variant

Debug.Print "hello"
Debug.Print Environ("PATH") 'Prints Nothing!

Set WshShell = CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PATH")
WshSysEnv("PATH") = WshSysEnv("PATH") & ";c:\Utils"

Debug.Print Environ("PATH") 'Prints Nothing!
Debug.Print "goodbye"
End Sub

(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
 
Reply With Quote
 
ChristopherL
Guest
Posts: n/a
 
      13th Aug 2008
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


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      13th Aug 2008
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
 
Reply With Quote
 
ChristopherL
Guest
Posts: n/a
 
      13th Aug 2008
On Aug 13, 7:31*am, Dave Peterson <peter...@verizonXSPAM.net> 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, youcould
> 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
 
Reply With Quote
 
ChristopherL
Guest
Posts: n/a
 
      13th Aug 2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment variable inside an environment variable murdo Microsoft Windows 2000 CMD Promt 7 25th Feb 2005 06:11 PM
Urgent: Creating an Environment Variable (System Variable)? =?Utf-8?B?U2F1cmFiaA==?= Microsoft Dot NET Framework 1 7th Feb 2005 05:51 PM
capture string output of command line application in an environment variable John Faughnan Windows XP General 1 5th Aug 2004 12:41 AM
setting a range variable equal to the value of a string variable Pilgrim Microsoft Excel Programming 2 1st Jul 2004 11:32 PM
OS Environment Variable Jon Windows XP General 3 2nd Dec 2003 05:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:37 PM.