有沒有辦法檢查 Excel 工作簿的版本是否為最新版本?

有沒有辦法檢查 Excel 工作簿的版本是否為最新版本?

我有一個主要的 Excel 工作簿,供使用者使用。此Excel工作簿的功能是使用巨集等複製和建立其他工作簿。該副本位於每個人都可以存取的網路磁碟機上。問題是,如果有人將此版本複製到他們的桌面,然後我後來想出此主工作簿的新版本,則該人複製到桌面的舊版本將不會有巨集等的最新更新。有沒有一種方法可以檢查或阻止舊工作簿運行巨集或使舊工作簿正常工作?

答案1

步驟1:您可以編寫一個巨集來檢查網路位置中的主檔案。您可以使用DirFSO來執行此操作:

目錄:

Sub Test_File_Exist_With_Dir()
    Dim FilePath As String
    Dim TestStr As String

    FilePath = "\\Server\test\book1.xlsm"

    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        MsgBox "File doesn't exist"
    Else
        MsgBox "File exist"
    End If

End Sub

飛行系統:

Sub Test_File_Exist_FSO_Late_binding()
'No need to set a reference if you use Late binding
    Dim FSO As Object
    Dim FilePath As String

    Set FSO = CreateObject("scripting.filesystemobject")

    FilePath = "\\Server\test\book1.xlsm"

    If FSO.FileExists(FilePath) = False Then
        MsgBox "file doesn't exist"
    Else
        MsgBox "File exist"
    End If

End Sub

Sub Test_File_Exist_FSO_Early_binding()
'If you want to use the Intellisense help showing you the properties
'and methods of the objects as you type you can use Early binding.
'Add a reference to "Microsoft Scripting Runtime" in the VBA editor
'(Tools>References)if you want that.

    Dim FSO As Scripting.FileSystemObject
    Dim FilePath As String

    Set FSO = New Scripting.FileSystemObject

    FilePath = "\\Server\Ron\test\book1.xlsm"

    If FSO.FileExists(FilePath) = False Then
        MsgBox "File doesn't exist"
    Else
        MsgBox "File exist"
    End If

End Sub

第2步:您可以讓它檢查該文件的最後修改日期,該日期可用於確定是否存在較新的版本。

FileDateTime("\\Server\test\book1.xlsm")

結果範例:2016 年 6 月 1 日 7:40:18 下午


步驟3:如果存在較新的版本,您可以向使用者顯示訊息框,以從網路磁碟機複製新版本並關閉工作簿。 (我不建議自動化從網路位置複製/貼上到用戶工作站,因為這很容易變得混亂,如果沒有這個,它仍然可以完成所需的工作)

MsgBox "A new version of this file exists on the network share. Please use the new version. This workbook will now close."
ActiveWorkbook.Close savechanges:=False

參考:

相關內容