Wednesday, May 26, 2010

How to get OS Version using VBA

How to retrieve Operating System Information using Excel/Word VBA

The version information of OS can be retrieved using the WIN API functions given below

Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
End Type

Private Declare Function GetVersionEx Lib "kernel32" _
      Alias "GetVersionExA" (lpVersionInformation As _
      OSVERSIONINFO) As Long


The following sub uses GetVersionEx function to get the Major and Minor version of OS

Sub Get_OS_Version_VBA()

' -------------------------------------------------------------
' Code to Get Version of Operating System through VBA
' -------------------------------------------------------------

Dim oOSInfo As OSVERSIONINFO
oOSInfo.dwOSVersionInfoSize = Len(oOSInfo)


GetVersionEx oOSInfo

' -------------------------------------------------------------
' Coded for http://vbadud.blogspot.com
' -------------------------------------------------------------

MsgBox "Version of Current OS is " & oOSInfo.dwMajorVersion & "." & oOSInfo.dwMinorVersion

End Sub

4 comments:

  1. Anonymous4:29 PM

    In VB 2010 express you can't use Private Type OSVERSIONINFO

    it has to be declared as a structure.

    How do you define those with out using a type?

    ReplyDelete
  2. Anonymous5:26 PM

    Same question! Anyone know?

    ReplyDelete
  3. Anonymous11:13 PM

    Instead of Private Type OSVERSIONINFO try Private Structure OSVERSIONINFO

    ReplyDelete
  4. Anonymous10:24 AM

    Public Function DirExists(pathName As String) As Boolean
    Dim oFSO As New FileSystemObject

    DirExists = oFSO.FolderExists(pathName)
    End Function

    If DirExists("C:\Program Files (x86)\.") Then
    a_Folder = Environ$("ProgramData")
    Else
    a_Folder = Environ$("ALLUSERSPROFILE")
    End If

    Here is an easy way to distinguish between Windows XP and Windows 7.

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...

Visual Basic for Applications (VBA) Forum (recent threads)

CodeKeep VBA Feed

Visual Studio Tools for Office Forum (recent threads)

Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

Office Business Applications (OBA) Team Blog

MSDN Code Gallery Published Resources For Tag VSTO

microsoft.public.vsnet.vstools.office Google Group