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

5 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
    Replies
    1. '***********************************************************************************************************************
      ' Navn : GetOSName
      ' Version : 1.0
      ' Dato : 30-09-2014
      ' Inparam : -
      ' Outparam : OS
      ' Beskrivelse : Finder og returnerer windows version
      ' TODO : -
      '***********************************************************************************************************************
      Public Function GetOSName()
      On Error GoTo Fejl
      Dim ObjWMIservice As Object, ColItems As Object, ObjItem As Object

      Set ObjWMIservice = GetObject("winmgmts:\\.\root\cimv2")
      Set ColItems = ObjWMIservice.ExecQuery("SELECT * FROM Win32_OperatingSystem", , 48)
      For Each ObjItem In ColItems
      GetOSName = ObjItem.Name
      Next

      GetOSName = Left(GetOSName, InStr(GetOSName, "|") - 1)
      GetOSName = Trim(MID(GetOSName, InStr(15, GetOSName, " "), 4))
      Select Case GetOSName
      Case 10: GetOSName = 10
      Case 8.1: GetOSName = 8
      Case 7.1, 7: GetOSName = 7
      Case Else: GetOSName = 5
      End Select
      Set ObjItem = Nothing
      Set ColItems = Nothing
      Set ObjWMIservice = Nothing
      ExitHer:
      Exit Function
      Fejl:
      MsgBox Err.Description, , ""
      Resume ExitHer
      End Function

      Delete

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