Determine if an Excel workbook has ever been saved?

I found this helpful. .BuiltinDocumentProperties("last save time") returns a vbDate (VarType = 7) representing the last time the workbook was saved ONLY IF the file has been saved at least once. Otherwise it returns a vbObject (VarType = 9).

Function WbSavedAtLeastOnce(ByVal target As Workbook) As Boolean
' Returns TRUE if the target workbook has been saved at least once.

    ' .BuiltinDocumentProperties("last save time") returns a vbDate
    '   only if the file has been saved at least once.
    WbSavedAtLeastOnce = VarType( _
            target.BuiltinDocumentProperties("last save time")) = 7
End Function

Use the function like this:

Sub Test()
    If WasSavedAtLeastOnce(ActiveWorkbook) Then
        MsgBox "This file has been saved at least once."
    Else
        MsgBox "This file has never been saved."
    End If
End Sub

The name (e.g. Book1) is a clue although I suppose someon could simply save their workbook with that name.

A clearer check may be as simple as a worksheet function that only works on a saved workbook.

'the following returns an empty string if used on an unsaved workbook
=CELL("filename", A1)

In VBA the .Address property will return no path when used with the External:=True argument.

?range("A1").Address(external:=true)
[Book1]Sheet5!$A$1

If ActiveWorkbook.Path = vbNullString Then
    'actions if the active workbook hasn't been saved yet go here
Else
    '....
End If

Tags:

Excel

Vba