How to get the version number of an Excel workbook?

此生再无相见时 提交于 2019-12-24 15:02:05

问题


I have an Excel book that's versioned in a Sharepoint document library, so that I can go to the File tab and see versions like:

19.0: 11/10/2014 1:15 PM by xyz\tkl2
17.0: 10/12/2014 3:54 PM by xyz\tkl2
14.0: 10/11/2014 2:23 PM by xyz\92jf

I want to retrieve the most recent version number, in this case 19.0. I've tried using the following code:

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
Next

End Sub

This is every property that seems possible to get regarding a particular document version. But none of these properties retrieve the actual version number; e.g., .Index would only retrieve 1, 2, and 3 for these versions. Is there a way to get to the actual version number?


回答1:


You can get this information by opening the historical version of the file, and its filename will be filename.xlsx, versionxx.yy:modified date, so that xx.yy will be the version number in the major.minor format.

I've put the code I used below. It puts the version name in column H of the sheet that it opens. It's got a little error checking in, but not enough to use straight off. Most importantly, it assumes that the spreadsheet it's pasted in is the only spreadsheet open. You'll want to have the file you want the version numbers of closed, too.

Function fCheckVersions(stFilename As String) As Boolean
'
' stFilename is the full URL to a document in a Document Library.
'
Dim wb As Excel.Workbook
Dim VersionWorksheet As Excel.Worksheet
Dim dlvVersions As Office.DocumentLibraryVersions
Dim dlvVersion As Office.DocumentLibraryVersion
Dim OldVersion As Excel.Workbook
Dim stExtension As String
Dim iPosExt As Long

viRow = 3
ThisWorkbook.Worksheets("Index").Cells(viRow, 1) = stFilename

If Workbooks.CanCheckOut(stFilename) = True Then
    Set wb = Workbooks.Open(stFilename, , True)
    Set dlvVersions = wb.DocumentLibraryVersions
    If dlvVersions.IsVersioningEnabled = True Then
        ThisWorkbook.Windows(1).Visible = False
        ThisWorkbook.Worksheets("Index").Cells(viRow, 3) = "Num"
        Versions = " & dlvVersions.Count"
        On Error GoTo VersionFailed:
        For Each dlvVersion In dlvVersions
            ThisWorkbook.Worksheets("Index").Cells(viRow, 4) = "Version: " & dlvVersion.Index
            ThisWorkbook.Worksheets("Index").Cells(viRow, 5) = "Modified Date: " & dlvVersion.Modified
            ThisWorkbook.Worksheets("Index").Cells(viRow, 6) = "Modified by: " & dlvVersion.ModifiedBy
            ThisWorkbook.Worksheets("Index").Cells(viRow, 7) = "Comments: " & dlvVersion.Comments
                Set OldVersion = dlvVersion.Open()
                ThisWorkbook.Worksheets("Index").Cells(viRow, 8) = "FileName: " & OldVersion.Name
                If Workbooks.Count > 2 Then
                    Workbooks(3).Close SaveChanges:=False
                End If
            viRow = viRow + 1
        GoTo NextVersion:
VersionFailed:
        ThisWorkbook.Windows(1).Visible = True
        MsgBox "Fail"
NextVersion:
        Next dlvVersion
    End If
    wb.Close False
End If
Set wb = Nothing
DoEvents
End Function


来源:https://stackoverflow.com/questions/27326121/how-to-get-the-version-number-of-an-excel-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!