Excel: Reading the First DOS Environment Variable

ID: Q69724


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("KERNEL","GetDOSEnvironment","C")
   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 "CURDIR" that you set to the current directory before running Microsoft Windows, using the macro example above, do the following:

  1. Type "SET" (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


  2. If you want to start Microsoft Windows from the C:\USER directory, type "SET CURDIR=C:\USER" (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


  3. Redeclare your other environment variables by using the "SET=" 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 "CURDIR=C:\USER."


REFERENCES

"Microsoft Excel Function Reference," version 4.0, pages 42-43, 350-352, 475-480

"Microsoft Excel Function Reference," version 3.0, pages 24, 193-194, 253-258

"Microsoft Excel for Windows Functions and Macros," version 2.1x, pages 255-256, 337-340

Additional query words: 2.1 2.10 3.0 3.00 4.0 4.00 5.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: March 21, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.