WD: Macro to Get Windows and Windows System Directories

ID: Q87706

The information in this article applies to:

SUMMARY

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

MORE INFORMATION

Word 6.x, 7.x

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.

Word 7.x

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Create a macro and place these declarations above the Sub Main routine:

   Declare Function GetSystemDirectoryA Lib "kernel32"(SystemDir$, \ 
   nSize As Long) As Long
   Declare Function GetWindowsDirectoryA Lib "kernel32"(WinDir$, nSize As \ 
   Long) As Long

   Sub MAIN
      'Get and display Windows directory
      a = GetWindowsDirectoryA(WindowsDir$, 255)
      MsgBox WindowsDir$, "Windows Directory", 64
      'Get and display Windows System directory
      a = GetSystemDirectoryA(SystemDir$, 255)
      MsgBox SystemDir$, "System Directory", 64
   End Sub

Word 6.x

Declare Function GetWindowsDirectory Lib "Kernel" \ 
(ReturnVal$, ReturnSize As Integer) As Integer

Declare Function GetSystemDirectory Lib "Kernel" \ 
(ReturnVal$, ReturnSize As Integer) As Integer

The following Word for Windows macro demonstrates how to use these calls to get and display the directory information:

   Declare Function GetWindowsDirectory Lib "Kernel" \ 
   (ReturnVal$, ReturnSize As Integer) As Integer

   Declare Function GetSystemDirectory Lib "Kernel" \ 
   (ReturnVal$, ReturnSize As Integer) As Integer

   Sub MAIN
      ' Get and display Windows directory
      a = GetWindowsDirectory(WindowsDir$, 255)
      MsgBox WindowsDir$, "Windows Directory", 64
      ' Get and display Windows System directory
      a = GetSystemDirectory(SystemDir$, 255)
      MsgBox SystemDir$, "System Directory", 64
   End Sub

Windows API syntax

WORD GetSystemDirectory(LPSTR lpBuffer, INT nSize)

 -and-

WORD GetWindowsDirectory(LPSTR lpBuffer, INT nSize)

In both cases, the result is returned in the string "lpBuffer," whose maximum size is specified by "nSize." Each routine returns the length of the string copied into lpBuffer.

REFERENCES

"Microsoft Windows Programmer's Reference," pages 4-210, 4-229

Kbcategory: kbusage kbmacro KBSubcategory: kbwordvba Additional query words: 1.0 1.10 1.10a 2.0 2.0a 2.0a-CD 2.0b 2.0c word6 7.0 word95 word7 winword 6.0 winword2

Keywords          : kbmacro kbmacroexample 
Version           : 1.x 2.x 6.0 6.0a 6.0c 7.0 7.
Platform          : WINDOWS

Last Reviewed: July 31, 1997