Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access built-in document properties information without opening the workbook

Tags:

excel

vba

I am using below code to get the created date of a workbook.

Dim mFile As String

mFile = "C:\User\User.Name\Test\Test.xlsx"
Debug.Print CreateObject("Scripting.FileSystemObject").GetFile(mFile).DateCreated

However to my surprise, this returns the date when the file is created in the directory.
If you copy the file to another folder, above will return that time and date it was copied (created).

To actually get the original created date, I tried using BuiltinDocumentProperties method.
Something like below:

Dim wb As Workbook
Set wb = Workbooks.Open(mfile) '/* same string as above */

Debug.Print wb.BuiltinDocumentProperties("Creation Date")

Above does return the original date the file was actually created.

Now, I have hundreds of file sitting in a directory that I need to get the original creation date.
I can certainly use above and look over the files, but opening and closing all of it from a shared drive takes some time. So I was wondering, if I can get the BuiltinDocumentProperties without opening the file(s) like using the first code above which is a lot faster and easier to manage.

If you somebody can point me to a possible solution, that would be great.

like image 429
L42 Avatar asked Jan 25 '18 05:01

L42


1 Answers

Try something like this. The key is the special DSO object.

Imports Scripting
Private Sub ReadProperties()
    Dim pathName As String = "C:\yourpathnamehere"
    Dim Fso As FileSystemObject = New Scripting.FileSystemObject
    Dim fldr As Folder = Fso.GetFolder(pathName)
    Dim objFile As Object = CreateObject("DSOFile.OleDocumentProperties")

    Dim ResValue As String = Nothing
    For Each f In fldr.Files
        Try
            objFile.Open(f)
            ResValue = objFile.SummaryProperties.DateCreated
            ' Do stuff here
            objFile.Close
        Catch ex As Exception
            'TextBox1.Text = ex.Message
        End Try

        Application.DoEvents()
    Next
End Sub
like image 54
ForEachLoop Avatar answered Sep 27 '22 16:09

ForEachLoop