Microsoft KB Archive/87706

{|
 * width="100%"|

WD: Macro to Get Windows and Windows System Directories

 * }

Q87706

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows, versions 7.0, 7.0a

-

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.

Word 6.x, 7.x
When Windows starts, it sets a special environment variable called &quot;windir&quot; 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$(&quot;windir&quot;)" 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 &quot;as is&quot; 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 &quot;kernel32&quot;(SystemDir$, \    nSize As Long) As Long Declare Function GetWindowsDirectoryA Lib &quot;kernel32&quot;(WinDir$, nSize As \   Long) As Long

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

Word 6.x
Declare Function GetWindowsDirectory Lib &quot;Kernel&quot; \ (ReturnVal$, ReturnSize As Integer) As Integer

Declare Function GetSystemDirectory Lib &quot;Kernel&quot; \ (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 &quot;Kernel&quot; \ (ReturnVal$, ReturnSize As Integer) As Integer

Declare Function GetSystemDirectory Lib &quot;Kernel&quot; \ (ReturnVal$, ReturnSize As Integer) As Integer

Sub MAIN ' Get and display Windows directory a = GetWindowsDirectory(WindowsDir$, 255) MsgBox WindowsDir$, &quot;Windows Directory&quot;, 64 ' Get and display Windows System directory a = GetSystemDirectory(SystemDir$, 255) MsgBox SystemDir$, &quot;System Directory&quot;, 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 &quot;lpBuffer,&quot; whose maximum size is specified by &quot;nSize.&quot; Each routine returns the length of the string copied into lpBuffer.