XL: Macros to Return Windows and System Directories Paths

Last reviewed: September 2, 1997
Article ID: Q108278
The information in this article applies to:
  • Microsoft Excel for Windows, version 5.0
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows

SUMMARY

In Microsoft Excel, there are no Visual Basic for Applications functions to determine either your \Windows or your \Windows\System directories.

NOTE: It may be possible to work around this situation by creating a macro that makes calls to the Windows application programming interface (API). This type of programming is supported by the Windows Software Development Kit (SDK) and the Visual Basic, Professional Edition, support groups. The level of support you can receive from these groups depends on the individual support policies of the group. (Microsoft support engineers may not be able to assist in specific construction of macros that use API programming.) If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

In Microsoft Excel, you can create a macro to return the path to the \Windows or \Windows\System directory by using the Declare Functions to access built-in functions in Microsoft Windows version 3.1, Windows 95, and Windows NT versions 3.51 and 4.0. The following macros use the GetWindowsDirectory and GetSystemDirectory function calls to retrieve the desired directory information.

Microsoft Excel versions 7.0 and 97

'The following two declare statements need to be entered each on a single
'line in the module sheet.

Declare Function GetSystemDirectory Lib "kernel32" Alias _
    "GetSystemDirectoryA" (ByVal lpBuffer As _
    String, ByVal nSize As Long) As Long

Declare Function GetWindowsDirectory Lib "kernel32" Alias _
    "GetWindowsDirectoryA" (ByVal lpBuffer As _
    String, ByVal nSize As Long) As Long

Sub Get_Windows_Directory()
    'sets the buffer length for both variables to 144
    Dim Win_Dir As String * 144
    Dim Sys_Dir As String * 144
    y = GetWindowsDirectory(Win_Dir, 144)
    'returns the \Windows directory
    wd = Left(Win_Dir, y)
    'Displays the windows directory in a Message box
    MsgBox wd
    'Returns the Windows\System directory
    x = GetSystemDirectory(Sys_Dir, 144)
    sd = Left(Sys_Dir, x)
    'Displays the \Windows\System directory in a Message box
    MsgBox sd
End Sub


Microsoft Excel version 5.0

'The following two declare statements need to be entered each on a single
'line in the module sheet.

Declare Function GetSystemDirectory Lib "KERNEL" (ByVal lpBuffer As _
    String, ByVal nSize As Integer) As Integer

Declare Function GetWindowsDirectory Lib "KERNEL" (ByVal lpBuffer As _
    String, ByVal nSize As Integer) As Integer

Sub GetDir()
    'sets the buffer length for both variables to 144
    Dim Win_Dir As String * 144
    Dim Sys_Dir As String * 144
    'returns the \Windows directory
    y = GetWindowsDirectory(Win_Dir, Len(Win_Dir))
    'Displays the windows directory in a Message box
    MsgBox Win_Dir
    'Returns the Windows\System directory
    x = GetSystemDirectory(Sys_Dir, Len(Win_Dir))
    'Displays the \Windows\System directory in a Message box
    MsgBox Sys_Dir
End Sub

REFERENCES

"Function Reference," version 4.0, pages 42, 350 On-line Help in the Microsoft Windows SDK


Additional query words: 5.00 7.00 8.00 97 XL97 API
Keywords : EvnOs kbprg PgmOthr
Version : 5.00 7.00 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.