Share via

VB code for obtaining file attributes

Anonymous
2014-04-22T08:32:31+00:00

Hi

From Excel I would like to access the attributes of a file whose path name and title I have within the code.

I would like to access the

Creation Date

Modified Date

Size

Once accessed, I would like to store them in cells B2, C2, D2.  Ideally I would not like the "modified date" to get changed when I access these attributes.

How could I do that via a VB code/macro in Excel?

Many thanks in anticipation

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-04-22T08:54:05+00:00

Hi,

Paste the following script to get your desired results:

Make sure to change strFilename location as your file location.

Sub GetDateCreated()

Dim oFS As Object

Dim strFilename As String

'Put your filename here

strFilename = "C:\Users\Name\Desktop\Book1.xlsm"

'This creates an instance of the MS Scripting Runtime FileSystemObject class

Set oFS = CreateObject("Scripting.FileSystemObject")

Range("B2") = oFS.GetFile(strFilename).DateCreated

Range("C2") = oFS.GetFile(strFilename).DateLastModified

Range("D2") = oFS.GetFile(strFilename).Size & " Bytes"

Set oFS = Nothing

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-09-20T13:47:12+00:00

    What if i want last author of any file?

    please give code for that.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-04-22T09:14:05+00:00

    You're welcome,

    You can replace D2 line with this code to get values in Kb instead of bytes.

     Range("D2") = Format(oFS.GetFile(strFilename).Size / 1024, "#,###.##") & " Kb"

    Please vote back as an answer if that was useful for you.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-04-22T08:55:17+00:00

    Thanks Ramz Aftab.  I will try this ASAP.

    Was this answer helpful?

    0 comments No comments