Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine Windows Version? 1

Status
Not open for further replies.

ktwclark

Programmer
Jan 30, 2002
54
GB
Simple question.

Is it possible to determine what version of Windows is running through Excel?
 
Go to Help, About Ms Excel, then System Info...

Or are you trying to create a formula?
 
ktwclark,

Use the following. Place it into a standard code module. Of course, you can use the returned info in something other than a msgbox. [wink]

Code:
Option Explicit

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

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

Dim Ver As OSVERSIONINFO


Sub WindowsVersion()
Dim retVal As Long
Dim msg As String

  Ver.dwOSVersionInfoSize = Len(Ver)
  retVal = GetVersionEx(Ver)
  If retVal = 0 Then
    msg = "An error ocurred while trying to get version information"
    MsgBox msg, vbOKOnly + vbExclamation, "Windows Version"
  Else
    msg = "Windows version number:  " & Ver.dwMajorVersion & "." & Ver.dwMinorVersion
    MsgBox msg, vbOKOnly + vbInformation, "Windows Version"
  End If
End Sub

HTH

M. Smith
 
This should work also.

Sub OS
MsgBox (Application.OperatingSystem)
End Sub
 
Here's yet another option, probably worth consideration because it provides a more descriptive result.

For example, with "Application.OperatingSystem", the result is: "Windows (32-bit) 4.90",
whereas with the following option, the result is:
"Windows Millennium [Version 4.90.3000]"

This places the result in the ActiveCell, but of course you can easily change that to suit your needs.

Sub WindowsVersion()
Application.ScreenUpdating = False
Shell "C:\winver.bat", vbHide
Workbooks.OpenText FileName:="C:\winver.txt", _
Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited
ActiveSheet.Range("A2").Copy
ActiveWindow.Close
ActiveSheet.Paste
Application.ScreenUpdating = True
End Sub

The above routine requires that you create the following batch file, and place it in C:\. The name and path are arbitrary, and you could change them.

name: winver.bat

content: ver > C:\winver.txt

Hope this helps. :)

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
Dale,

I like your solution; much more descriptive! May I suggest the following modification, making your procedure self-contained. It creates the needed batch file on the fly.

Code:
Sub WindowsVersion()
Const BatchCommand As String = "Ver >C:\winver.txt"
Dim FNum As Integer
Dim VersionStr As String

    FNum = FreeFile
    Open "C:\winver.bat" For Output As FNum
    Print #FNum, BatchCommand
    Close #FNum
    Shell "C:\winver.bat", vbHide
    FNum = FreeFile
    Open "C:\winver.txt" For Input As FNum
    Line Input #FNum, VersionStr  'Blank line
    Line Input #FNum, VersionStr
    Close #FNum
    Kill "C:\winver.bat", "C:\winver.txt"
    MsgBox VersionStr, vbOKOnly + vbInformation, "Windows Version"
End Sub

Another approach is to execute the Ver command directly using Shell. However, this is complicated by the fact that depending on the Windows platform, the command interpreter to invoke could be either command.com or cmd.exe (you would need to know which version of Windows is running to determine which version of Windows is running!)

Regards,
Mike
 
Mike,

"Teamwork"... I love your modification !!!

...Just one slight problem - that I'm hoping you can rectify.

When I run the routine the FIRST time, NO problem. However, when I run it a SECOND time, it returns "File not found" for the line...

Open "C:\winver.txt" For Input As FNum

Interestingly, when I "step" through the routine, I can do so multiple times WITHOUT any problem. (Is this Microsoft logic, or is there some other explanation ?)

I also encountered a similar problem with the line...

Kill "C:\winver.bat", "C:\winver.txt"

...but I seem to have cured this by splitting it - i.e.

Kill "C:\winver.bat"
Kill "C:\winver.txt"

Hope you can "wave your magic wand over this". :)

Regards, ...Dale
 
Dale,

You are quite right! Regarding the Kill statement: I threw that in at the last minute w/o testing...must have read the documention incorrectly. The other problem is much more serious. I believe it stems from the fact that the Shell command runs asynchronously so that the remaining code continues to run even though the batch file may not have completed. I attempted to alleviate this using some error handling (simply resuming at the
Code:
Open "C:\winver.txt" For Input As FNum
statement). This helped but did not eliminate the problem completely. Actually, it's more accurate to say that another problem ocurred. I started getting a "read past end of file" error, as if the Line Input's were reading the text file for a second time. I can't quite figure that one out. Because of these issues, I don't recommend using my "improvement". Oddly enough, the procedure worked fine on my home system (non-networked, if that makes any difference). I'll continue to look at this. If you or anyone else has any ideas, please post them. Thanks

Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top