Microsoft KB Archive/69724

{|
 * width="100%"|

Excel: Reading the First DOS Environment Variable

 * }

-

The information in this article applies to:


 * Microsoft Excel for Windows 95, version 2.x, 3.0, 4.0, 5.0

-

IMPORTANT: The CALL and REGISTER functions are provided for advanced users only. If you use these functions incorrectly you could accidentally cause errors in your system's operation.

SUMMARY
It is possible to retrieve the value of the first environment variable from the DOS environment string from within Microsoft Excel if you are running under Microsoft Windows. This is done using the CALL and REGISTER functions. One possible use for this would be to determine the directory from which Microsoft Windows was started.

MORE INFORMATION
The DOS environment is an area of memory that DOS sets aside to store a series of ASCII strings. Each string in the environment is terminated by a byte of zero. The final string is terminated by two bytes of zero.

The following Microsoft Excel macro returns the first substring in the MS-DOS environment string:

  A1      Get_Environment A2     =REGISTER(&quot;KERNEL&quot;,&quot;GetDOSEnvironment&quot;,&quot;C&quot;) A3     =CALL(A2) A4     =RETURN

Note: The CALL function can also be used on a standard worksheet in Microsoft Excel versions 3.0 and later.

Adding Variable to Beginning of Environment String
To arrange for a particular string to be placed at the beginning of the environment string, you must redefine all environment variables in a particular order. When an environment variable is first defined, its string is appended to the end of the existing environment string. If the variable was previously defined, it is deleted from its old position in the string and appended to the end.

To retrieve an environment string called &quot;CURDIR&quot; that you set to the current directory before running Microsoft Windows, using the macro example above, do the following:


 * 1) Type &quot;SET&quot; (without the quotation marks) at the MS-DOS prompt to display the current environment string. For this example, the SET command displays:

COMSPEC=C:\DOS\COMMAND.COM

PATH=C:\WINDOWS;C:\;C:\WINWORD

TEMP=C:\WINDOWS\TEMP
 * 1) If you want to start Microsoft Windows from the C:\USER directory, type &quot;SET CURDIR=C:\USER&quot; (without the quotation marks) at the MS-DOS prompt and press the ENTER key. The SET command now returns the following:

COMSPEC=C:\DOS\COMMAND.COM

PATH=C:\WINDOWS;C:\;C:\WINWORD

TEMP=C:\WINDOWS\TEMP

CURDIR=C:\USER
 * 1) Redeclare your other environment variables by using the &quot;SET=&quot; command with COMSPEC, PATH, and TEMP just as they appeared previously. The SET command now returns the following:

CURDIR=C:\USER

COMSPEC=C:\DOS\COMMAND.COM

PATH=C:\WINDOWS;C:\;C:\WINWORD

TEMP=C:\WINDOWS\TEMP

Now run Microsoft Windows and Microsoft Excel. When you run the above macro, cell A3 returns &quot;CURDIR=C:\USER.&quot;