Astra

Excel VBA File Properties

Estimated reading: 4 minutes

In this tutorial, we will learn to show, save and modify the file properties by Excel VBA

Example 1: Show the File Properties of the Current Workbook by VBA 

The following code will show the file properties in a message box 

Sub Show_Properties()
    Dim wb As Workbook
    Set wb = ThisWorkbook 'or set it to another workbook using Workbooks.Open or Workbooks.Add
    
    'Read the built-in document properties
    Dim msg As String
    msg = "Title: " & wb.BuiltinDocumentProperties("Title") & vbCrLf & _
          "Author: " & wb.BuiltinDocumentProperties("Author") & vbCrLf & _
          "Subject: " & wb.BuiltinDocumentProperties("Subject") & vbCrLf & _
          "Keywords: " & wb.BuiltinDocumentProperties("Keywords") & vbCrLf & _
          "Comments: " & wb.BuiltinDocumentProperties("Comments") & vbCrLf & _
          "Last saved by: " & wb.BuiltinDocumentProperties("Last Author") & vbCrLf & _
          "Last saved on: " & wb.BuiltinDocumentProperties("Last Save Time")
    
    'Display the properties in a MsgBox
    MsgBox msg, vbInformation, "File Properties"

Result: Properties of the current workbook will be shown in the message box as shown below

Excel VBA File Properties

Example 2: Modify the File Properties  by VBA 

Suppose you want to modify the file properties according to the properties enlisted in the Excel sheet given below

Just use the code given below

Sub Modify_Properties()
    Dim wb As Workbook
    Set wb = ThisWorkbook 'or set it to another workbook using Workbooks.Open or Workbooks.Add
    
    'Read the built-in document properties
    Debug.Print "Title: " & wb.BuiltinDocumentProperties("Title")
    Debug.Print "Author: " & wb.BuiltinDocumentProperties("Author")
    Debug.Print "Subject: " & wb.BuiltinDocumentProperties("Subject")
    Debug.Print "Keywords: " & wb.BuiltinDocumentProperties("Keywords")
    Debug.Print "Comments: " & wb.BuiltinDocumentProperties("Comments")
    Debug.Print "Last saved by: " & wb.BuiltinDocumentProperties("Last Author")
    Debug.Print "Last saved on: " & wb.BuiltinDocumentProperties("Last Save Time")
    
    'Modify the built-in document properties
    wb.BuiltinDocumentProperties("Title") = "Solved Excel"
    wb.BuiltinDocumentProperties("Author") = "Solved Excel"
    wb.BuiltinDocumentProperties("Subject") = "Your Excel Coach"
    wb.BuiltinDocumentProperties("Keywords") = "Excel"
    wb.BuiltinDocumentProperties("Comments") = "Find Easy Solution"
    
    'Save the changes
    wb.Save
 MsgBox "File Properties Changed", vbInformation, "File Properties"
End Sub

Example 3: Get the File Properties from Given Path  and Save in the Current Workbook by VBA 

 Use the following VBA code to get the file properties from a given file path and then save in the current Excel workbook

Sub Get_File_Properties()

    'Specify the file path
    Dim filePath As String
    filePath = ThisWorkbook.Sheets(3).Range("G6").Value

    'Select the worksheet to save the file properties
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets(3)

    'Set column headers
    ws.Range("A1").Value = "Property"
    ws.Range("B1").Value = "Value"

    'Declare variables
    Dim Shellobj As Object
    Dim Folderobj As Object
    Dim FolderItemobj As Object
    Dim i As Integer
    
    'Create a Shell object
    Set Shellobj = CreateObject("Shell.Application")
    
    'Get the folder object
    Set Folderobj = Shellobj.Namespace(Left(filePath, InStrRev(filePath, "\")))
    
    'Get the folder item object for the file
    Set FolderItemobj = Folderobj.ParseName(Right(filePath, Len(filePath) - InStrRev(filePath, "\")))
    
    'Loop through the properties and add them to the worksheet
    For i = 0 To 39
        ws.Range("A" & i + 2).Value = Folderobj.GetDetailsOf(Folderobj.Items, i)
        ws.Range("B" & i + 2).Value = Folderobj.GetDetailsOf(FolderItemobj, i)
    Next i
    
    'Save the workbook
    ThisWorkbook.Save

End Sub

Result: After running the code, all properties are enlisted in sheet 1

Excel VBA File Properties

Note: Please check the following references to run the code

Excel VBA File Properties

3 thoughts on “Excel VBA File Properties”

  1. I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all web owners and bloggers made good content as you did, the net will be a lot more useful than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc
Jump to Topics
SOLVED EXCEL
Scroll to Top