OFF2000: Macro to Get Windows and Windows System Folders

This article was previously published under Q200123
This article has been archived. It is offered "as is" and will no longer be updated.
SUMMARY
The Microsoft Windows operating system provides two routines that can becalled by Microsoft Office 2000 programs to get the \Windows and the \Windows\System folders.
MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. When Windows starts, it sets a special environment variable calledWINDIR with the path from which Windows was started. You can use thefollowing macro instruction to post the Windows folder to a message box.
MsgBox Environ$("WINDIR")					
However, there is no argument to return the \System folder whenusing this method. The following examples use Windows API calls to returnboth the \Windows and \Windows\System folders.

The following macro example retrieves the \Windows and \Windows\System folders and prints them to the Debug (Immediate) window in theVisual 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 forApplications Editor, on the View menu, click Immediate Window (or DebugWindow).
OFF2000 off2k vb vba vbe
Properties

Article ID: 200123 - Last Review: 02/21/2014 00:31:27 - Revision: 5.2

Microsoft Excel 2000 Standard Edition, Microsoft Access 2000 Standard Edition, Microsoft PowerPoint 2000 Standard Edition, Microsoft Word 2000 Standard Edition

  • kbnosurvey kbarchive kbhowto KB200123
Feedback