Retrieving the Excel file detail "Version number"

I have a spreadsheet that gets updated when features are added or comparison data gets changed. Each time, the "Version number" field in the file properties details gets changed to reflect its configuration. I'd like to put the value of that field in a cell, so that when the spreadsheet is printed, the current version number is on the title page, so that the reviewer can ensure that the current version of the spreadsheet was used.

I'm doing this manually, now, but sometimes I forget and they get out of sync. I could use the Workbook_Open() event to get it and put it into the cell on opening, if I could figure out how to get to the property detail. ThisWorkbook.BuiltinDocumentProperties("Revision Number") works, but it can't find "Version" or "Version number", and Revision wants an integer, not the major.minor string that we're using.

Is there a method/property that does this?
jtrawickSenior Systems EngineerAsked:
Who is Participating?
Glenn RayConnect With a Mentor Excel VBA DeveloperCommented:
Yes.  The example below is a subroutine in the "ThisWorkbook" object that checks for a custom property.  You'll add your own code to retrieve or insert the [LATEST VERSION NUMBER]
Private Sub Check_Version()
    Dim Ver As Object
    Set Ver = ActiveWorkbook.CustomDocumentProperties.Item("Version")
            MsgBox "A new version of this application is now available." & vbLf & _
                "You are running version number " & Ver & vbLf & _
                "The latest version number is " & [LATEST VERSION NUMBER],  vbCritical + vbOKOnly, "Upgrade Notice"
            Application.DisplayAlerts = False
            Application.DisplayFullScreen = False
        End If
    End If    
End Sub

Open in new window

xtermieConnect With a Mentor Commented:
There is an extra End if there I think on line 12.
If you have several versions of your spreadsheet you may want to consider a SharePoint document library that will provide with proper versioning as well (minor and major).

In that case you can retrieve version info as follows:
Sub getVersions()

Dim DocVersions As DocumentLibraryVersions
Dim DVersion As DocumentLibraryVersion

Set DocVersions = ThisWorkbook.DocumentLibraryVersions

For Each DVersion In DocVersions
    Debug.Print DVersion.Index
    Debug.Print DVersion.Comments
    Debug.Print DVersion.Creator
    Debug.Print DVersion.Modified
    Debug.Print DVersion.ModifiedBy
    Debug.Print DVersion.Application

End Sub

Open in new window

Glenn RayConnect With a Mentor Excel VBA DeveloperCommented:
^Yes; line 12 should be removed from my example.  I was just stripping it out of some existing code.

The key code to note are lines 2-3 where one can access a custom document property with the name specified in quotes.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jtrawickSenior Systems EngineerAuthor Commented:
When I run the "set ver" line (3), I get "Run time error 5: Invalid procedure call or argument". I get the same error with ".BuiltinDocumentProperties("Version")"

.BuiltinDocumentProperties("Revision Number") works, but it's not what I need (wrong metadata element).

It seems like it doesn't understand the "Version" index in those collections.

I can set a custom property, but I'd like to use what Excel provides.
Glenn RayConnect With a Mentor Excel VBA DeveloperCommented:
If your Excel workbook does not already have a Document Property tltled "Version", yes you will get an error.

You can add new Custom Properties through the Advanced Properties menu:
  • File
  • Info page
  • Properties tab (on far right) - click drop arrow
  • Advanced Properties
  You'll see a new dialog box pop up
  • "Custom" tab
You'll see several built-in properties that you can use, but you can also add your own by typing a new name in the "Name:" field, choosing the type of data in the "Type" field, and then entering the property value in the "Value" field.  See the below example.
Excel document properties access
jtrawickSenior Systems EngineerAuthor Commented:

I can set a custom property, but I'd like to use what the OS provides.
Glenn RayConnect With a Mentor Excel VBA DeveloperCommented:
I apologize for the confusion; I didn't realize you were referring to the extended file attributes.

I'm thinking an API call will be necessary here, but am not sure which one yet.  You stated that the Version number value is changed when the file is updated.  I don't know a setting in Excel that will update that value, so is the file already using VBA?  If so, we'd just need to locate the code that updates the value and then modify it to extract the Version number.

I noted while I was experimenting with other existing code that:
1) Excel files of version 2007 or earlier (.xls) did not allow manual editing of either the Revision number or Version number in Explorer
2) The "File Version" constant (which may or may not be the same as "Version number" is dependent on the OS.  

I'm still digging; think it might be possible using Shell.Application.
jtrawickSenior Systems EngineerAuthor Commented:

The spreadsheet uses fairly extensive VBA, and I can count on Windows 7 as the OS and Office 2013. The spreadsheet doesn't get a new version every time, only when the logic or the reference tables are changed. I can set the version in the code, or on the "extended file attributes", but they need to stay synced, so I need to do it in one place. If a create a new variable, it still needs to end up in the "extended file attributes 'Version number'", so I either need to read it or write it, or continue to be very careful (which is not my nature).

Hadn't thought about an OS shell API (now I feel REALLY stupid...) will check in that direction, also. .GetDetailsOf looks promising.
Glenn RayExcel VBA DeveloperCommented:
Unfortunately, the .GetDetailsOf appears to only reference the internal BuiltIn or Custom document properties, not OS or NTFS data.

If you have the VBA code that writes the extended file "Version number", I'd use that as a basis for the method of writing that same value to a cell.  Plus, I'd be really interested in seeing that code... :-)
jtrawickSenior Systems EngineerAuthor Commented:
No code - I've been using the right-click to set it (left-click in the box). No idea what that does under the covers.

I went through the first 900 of .GetDetailsOf, no success. 26 of them have data, the last being in index 269, though I can only find descriptions for about 15, Audio/video/picture files have a lot more.

Why does MS make it so hard to get metadata that THEY provide containers for?

I'll let this go for a couple more days, then I'll have to brute-force it. I think I remember liking Microsoft...
Glenn RayConnect With a Mentor Excel VBA DeveloperCommented:
I had created a quick utility workbook to retrieve all metadata for a specific file that I manually updated the Revision number & Version number in Windows (to 666 and 667)
extended properties
I found that the upper limit is 292 and NONE of the items or values matched. :-(

Sub getproperties()
    Dim sFile As Variant
    Dim oShell: Set oShell = CreateObject("Shell.Application")
    Dim oDir:   Set oDir = oShell.Namespace("C:\Misc - Excel Development\Excel Tips Tools Tricks\Expert Exchange Work\Test\")
    'Need to modify this to pick a single file (or current file)
    For Each sFile In oDir.items
        If sFile.Name = "Another file (2).xlsx" Then
            For i = 0 To 292
                ActiveCell.Value = i
                ActiveCell.Offset(0, 1).Value = sFile.Name
                ActiveCell.Offset(0, 2).Value = oDir.getdetailsof(oDir.items, i)
                ActiveCell.Offset(0, 3).Value = oDir.getdetailsof(sFile, i)
                ActiveCell.Offset(1, 0).Select
        End If

End Sub

Open in new window

jtrawickSenior Systems EngineerAuthor Commented:
Sorry. Been out with the "flu" (doc's opinion, not mine) for a couple of weeks.

This is in accord with my own test. No Joy.

Glenn Ray, thanks for the help in finding that there is no solution. Would like to give you credit for that, but it won't let me click "Assisted Solution" without a "Best Solution".
Glenn RayExcel VBA DeveloperCommented:
I believe you can close this question without awarding a solution, but you'll have to check with an admin to find out.

Sorry that I couldn't have been of more assistance.
good comments and code provided by Experts
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.