OFF97: Macro to Get Windows and Windows System Directories

Last reviewed: June 27, 1997
Article ID: Q170728
The information in this article applies to:
  • Microsoft Visual Basic for Applications included with:

        - Microsoft Word 97 for Windows
        - Microsoft Access versions 7.0, 97
        - Microsoft Excel 97 for Windows
        - Microsoft PowerPoint 97 for Windows
    

SUMMARY

The Microsoft Windows operating system provides two routines that can be called by Microsoft Office 97 programs to get the Windows and the Windows System directories.

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.

When Windows starts, it sets a special environment variable called "windir" with the path from which Windows was started. You can use the following macro instruction to post the Windows directory to a message box.

   MsgBox Environ$("windir")

However, there is no argument to return the Windows System directory when using this method. The following examples use Windows API calls to return both the Windows and Windows System directories.

The following macro example retrieves the Windows and Windows System directories and prints them to the Debug (Immediate) window inside the Visual Basic for Applications Editor.

' Place these declarations in the General Declarations procedure of a
' Visual Basic for Applications module.

   Declare Function GetWindowsDirectoryA Lib "Kernel32" _
   (ByVal lpBuffer As String, ByVal nSize As Long) As Long
   Declare Function GetSystemDirectoryA Lib "Kernel32" _
   (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Create a macro and place the following code into the routine.

   Sub GetOSDirs()
      Dim sBuf As String
      Dim cSize As Long
      Dim retval As Long
      sBuf = String(255, 0)
      cSize = 255

      ' Get Windows Directory.
      retval = GetWindowsDirectoryA(sBuf, cSize)
      sBuf = Left(sBuf, retval)
      Debug.Print sBuf

      ' Get System Directory.
      sBuf = String(255, 0)
      cSize = 255
      retval = GetSystemDirectoryA(sBuf, cSize)
      sBuf = Left(sBuf, retval)
      Debug.Print sBuf
   End Sub

If the Debug window is not visible, then while in the Visual Basic for Applications Editor, on the View menu, click Immediate Window (or Debug Window).


Additional query words: 8.00 8.0 offcon vb vba vbe
Keywords : kbcode
Version : 97
Platform : WINDOWS
Issue type : kbhowto


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: June 27, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.