Excel

Batch Change Workbook Properties

Ease of Use

Easy

Version tested with

2000, 2002 

Submitted by:

Justinlabenne

Description:

Changes every workbook in a folders built-in document properties 

Discussion:

If you have a lot of workbooks and you need to make changes to the built-in document properties (Author, Title, Comments, Subject, etc) this code will update all the workbooks contained inside a single folder in one shot. You will need to specify the changes you want in the code, and what properties you want to change. The example changes the Author, Title, and Comments property. The code allows you to browse for the folder containing the workbooks to update. 

Code:

instructions for use

			

Option Explicit Sub ChangeLotsOfFilesProperties() ' Attributes we will be changing ' Author, Title, Comments Const szAuthor As String = "vbaexpress.com" Const szTitle As String = "Updated Title" Const szComments As String = "Batch update code" Dim szFolderPath As String Dim objFolder As Object Dim szbkName As String Dim lUbk As Long Dim i As Long Dim wkb As Workbook Dim fso As Object Dim f As Object ' Browse for the folder to search for project workbooks ' =========================================================================== Set objFolder = CreateObject("Shell.Application"). _ BrowseForFolder(0, _ "Select the folder containing workbooks to update", _ 0, Empty) On Error GoTo ErrExit If Not objFolder Is Nothing Then szFolderPath = objFolder.items.Item.Path Else Exit Sub End If ' =========================================================================== With Application .ScreenUpdating = False .EnableEvents = False If Val(.Version) >= 9 Then 'ShowWindowsInTaskbar is for versions 2000+ .ShowWindowsInTaskbar = False End If End With With Application.FileSearch .NewSearch .LookIn = szFolderPath .SearchSubFolders = False .Filename = "*.xls" .MatchTextExactly = True .FileType = msoFileTypeExcelWorkbooks .Execute ' if we found some files to update If .FoundFiles.Count > 0 Then ' Loop through them, changing document properties For i = 1 To .FoundFiles.Count Set wkb = Workbooks.Open(.FoundFiles(i)) ' Procedure can be lengthy, status bar for updating Application.StatusBar = "[" & i & " of " & _ .FoundFiles.Count & "] Changing properties for " & wkb.Name ' Late binding reference to the FileSystemObject Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFile(ActiveWorkbook.FullName) ' If the file is Read-Only, don't update it If f.Attributes And 1 Then ' so close it: wkb.Close False Else ' Otherwise change the specific document properties With wkb ' Props we are changing, Author, Title, Comments .BuiltinDocumentProperties("Author") = szAuthor .BuiltinDocumentProperties("Title") = szTitle .BuiltinDocumentProperties("Comments") = szComments ' Store the workbook names we update in a variable ' This will be used to deliver our final message szbkName = szbkName & vbNewLine & wkb.Name ' Then save and close .Save .Close End With End If Next i Else MsgBox "No files found to update", 64 End If End With ErrExit: ' Explicitly clear memory Set wkb = Nothing Set fso = Nothing Set f = Nothing Set objFolder = Nothing With Application .ScreenUpdating = True .EnableEvents = True If Val(.Version) >= 9 Then 'ShowWindowsInTaskbar is for versions 2000+ .ShowWindowsInTaskbar = True End If .StatusBar = Empty End With MsgBox "* Properties have been changed for these Files: *" & szbkName, 64 End Sub

How to use:

  1. Open an Excel Workbook
  2. Copy the code
  3. Press Alt + F11 to open the Visual Basic Editor (VBE)
  4. Select INSERT > MODULE from the menubar
  5. Paste code into the right pane
  6. Press Alt+Q to return to Excel
  7. Save workbook before any other changes
 

Test the code:

  1. Go to TOOLS > MACRO > MACROS
  2. When the dialog appears, select [ChangeLotsOfFilesProperties]
  3. Press Run
 

Sample File:

BatchChangeProps.zip 9.45KB 

Approved by mdmackillop


This entry has been viewed 162 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express