logo ASAP Utilities

Excel tip: Get the windows directory

Date: 5 september 2001

The following function will give you the directory where MS Windows is installed. This code is very usefull because the directory is not always "c:\windows", i.e. for Windows 2000 this is "c:\winnt".


Copy-paste friendly code:
Option Explicit

'Windows API/Global Declarations
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" _
                                             (ByVal lpBuffer As String, _
                                              ByVal nSize As Long) As Long


Function fGetWinDir() As String
      Dim lRet                               As Long
      Dim lSize                              As Long
      Dim sBuf                               As String * 260            '260 = max path
      lSize = 260
      lRet = GetWindowsDirectory(ByVal sBuf, ByVal lSize)
      If InStr(1, sBuf, Chr(0)) > 0 Then
            fGetWinDir = Left(sBuf, InStr(1, sBuf, Chr(0)) - 1)
      Else
            fGetWinDir = ""
      End If
End Function


Sub TestDisplayWinDir()
      MsgBox "Your windows directory is:" & vbNewLine & _
             fGetWinDir(), vbInformation, "Windows folder"
End Sub



« back
Share this page:
Home Sitemap Search List of all utilities Printer friendly page ^ Back to Top
© 1999-2017 - - - - The Netherlands

Connect with us and receive our tips that show you how to benefit from the tools in ASAP Utilities at:
Follow us on TwitterFollow us on Facebook