|
|
|
|
|
|
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()
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
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 = False
End If
End With
With Application.FileSearch
.NewSearch
.LookIn = szFolderPath
.SearchSubFolders = False
.Filename = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
.Execute
If .FoundFiles.Count > 0 Then
For i = 1 To .FoundFiles.Count
Set wkb = Workbooks.Open(.FoundFiles(i))
Application.StatusBar = "[" & i & " of " & _
.FoundFiles.Count & "] Changing properties for " & wkb.Name
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(ActiveWorkbook.FullName)
If f.Attributes And 1 Then
wkb.Close False
Else
With wkb
.BuiltinDocumentProperties("Author") = szAuthor
.BuiltinDocumentProperties("Title") = szTitle
.BuiltinDocumentProperties("Comments") = szComments
szbkName = szbkName & vbNewLine & wkb.Name
.Save
.Close
End With
End If
Next i
Else
MsgBox "No files found to update", 64
End If
End With
ErrExit:
Set wkb = Nothing
Set fso = Nothing
Set f = Nothing
Set objFolder = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
If Val(.Version) >= 9 Then
.ShowWindowsInTaskbar = True
End If
.StatusBar = Empty
End With
MsgBox "* Properties have been changed for these Files: *" & szbkName, 64
End Sub
|
How to use:
|
- Open an Excel Workbook
- Copy the code
- Press Alt + F11 to open the Visual Basic Editor (VBE)
- Select INSERT > MODULE from the menubar
- Paste code into the right pane
- Press Alt+Q to return to Excel
- Save workbook before any other changes
|
Test the code:
|
- Go to TOOLS > MACRO > MACROS
- When the dialog appears, select [ChangeLotsOfFilesProperties]
- Press Run
|
Sample File:
|
BatchChangeProps.zip 9.45KB
|
Approved by mdmackillop
|
This entry has been viewed 162 times.
|
|