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)
            fGetWinDir = ""
      End If
End Function

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

« back

Home Privacy Policy Cookie Policy EULA Download All added Excel tools Sitemap Contact Us

Empowering Excel Users Worldwide for 25 Years