|
My Rename a Batch of
Files Microsoft Word Help & Tips page provides some basic code and a
practical example of using a VBA routine to process a batch of Word
documents stored in a common folder.
This Microsoft Word Help & Tips page is
the result of a collaborative effort by fellow Word MVP Doug Robbins and
myself to expand
and enhance the method decribed in that article. Thanks also to fellow
MVP Graham Mayor for his
exhaustive assistance showing me where things where wrong or could go wrong.
Here I will provide
two Word AddIns that work in tandem to make review and processing a batch of
Word files relatively
effortless. One AddIn "BatchProcessDocuments.dot" contains the VBA
routines to allow the user to select a folder for processing and the
processing method (Automatic or Manual). The second AddIn
"BatchProcesses.dot" contains the VBA routines to perform on the document.
Both AddIns must be loaded for the pair to function. You can store the
AddIns in your Word Templates directory and load them using the Templates and
Add-ins Dialog as shown below. |
|

|
|
The AddIns were created using Word
2003. I used Word2003 in order to create a simple toolbar in BatchProcessDocuments.dot
which includes two commands as shown on the left. These commands will manifest as command button controls
in the "Custom Toolbars" group of the Add-In tab of the Word2007 User
Interface Ribbon as shown on the right. |
 |
 |
|
When the user selects either command button, a
dialog appears which allows the user to select the folder containing
the batch of files to process. In the illustration shown below
I have selected a folder named "Batch." I have placed all the
files that I want to process in this folder. When the folder
to process is selected then click "OK" on the bottom right of the
dialog. |
|

|
|
If the user selected Batch Auto Process, a
UserForm will appear like the one shown below. This form shows
the folder selected for processing and allows the user to select one
of three pre-defined processes or a user defined process. The
processes themselves are part of the "BatchProcesses.dot" AddIn
which I will discuss later. The "Convert Date Fields" process
will change all {DATE} fields in a document to {CREATEDATE} fields.
"Find and Replace" performs a find and replace operations on
each file in the batch of files. You can enter multiple find and
replace pairs to process. "Convert .doc to .docx"
(shown here as disabled) is only available
when the AddIns are loaded in a Word2007 application and converts
each file in a
batch of files from .doc format to .docx format. |
|

|
|
After the user has selected a process and
entered other relevant data the "Begin Processing" command in
enabled as shown in the illustration below. Users
then click on "Begin Processing" to start processing. |
|

|
Note:
the "Add F/R pair" command is used to perform multiple find and replace operations as each file is
processed. For example, say I want to update a batch of White
House documents.
I would enter the first find and replace pair as shown above and
click "Add Pair." I then enter a second pair of find and replace
with terms e.g., find: 43rd, replace with: 44th. If the
new pair is valid then the "Begin Processing" command is enabled
and you can begin processing or add another pair.
The pair showing in the find and replace with fields are
automatically part of the collection of terms to process, so only use the "Add F/R pair"
command to add additional pairs. |
|
As the documents are processed the UserForm
updates to show progress. If the folder
selected for processing contains sub-folders, the user will be presented an option
to
continue processing in sub-folders after the last file in selected batch
folder is processed. |
|

|
|
When the process is completed the user is
presented a log file of the documents processed and the UserForm
display updates accordingly. |
|

|
Warning: While
I have attempted to make these AddIn as comprehensive as
possible, I am fairly certain that they are not foolproof or perfect. You should be careful to
"always" save a copy of
the files you
wish to process in a separate folder prior to processing any
files with these AddIns.
Note:
If your batch folder contains open password or
modify password protected
files then you will need to supply the password as the AddIn
attempts to open each document for processing.
Note:
There are a couple of minor unresolved glitches in
my code for handling invalid passwords. One in particular manifests in Word2007 as a
run-time error and crashes Word in the event you
click "Cancel" in the Password dialog box of a password
protected file. You can avoid these glitches by removing passwords
prior to processing or being certain of your passwords before you start. Sorry! |
|
If the user selected Batch Manual Process, a
UserForm will appear like the one shown below. |
|

|
|
This process opens each document in the selected batch
folder one at a time and presents it on the screen for the user to review or
manually process. In addition to performing manual processes to the
open document, a command button is available for executing a user defined
auto process (the auto process is defined and stored in the
BatchProcesses.dot AddIn). When the user is finished processing the
document they click "Next File." This automatically saves changes,
closes the document, and presents the next document for processing.
The UserForm interface notifies the user when the last file in the batch
folder is presented by changing the "Next File" command button caption to
"Finished."
Note:
There is no log feature or capability to continue processing in sub-folders
when using the Manual Process method.
Notice
the UserForm caption is "dimmed." This is a modeless form
that is displayed on the surface of the document but it doesn't have the focus.
This lets you see the form and at the same time perform document processes.
Clicking a command button in the UserForm returns the focus to the form. |
|
Separating the code to select the batch folder
and sequence through each file from the code that actually performs
the process on the document was intentional to minimize unneccary
access and tampering with the code in BatchProcessDocuments.dot. |
|
As mentioned earlier, the code for performing
processes on each document is contained in the second AddIn
"BatchProcesses.dot." With this distribution BatchProcess.dot
contains the three pre-defined processes "Convert Date Fields," "Find
and Replace," and "Convert .doc to .docx;" and a placeholder for a
user defined process. Users can readily change the user
defined process to meet varying needs. The following two code
snippets shows the existing user defined process and an example of
how it code be edited for a useful purpose (e.g., change a style
attribute). |
Function User_Defined(ByRef Doc As Word.Document) As
String
On Error GoTo Err_Handler
'*******
'Put your document processing code here. E.g.:"
MsgBox Doc & " auto process complete."
'*******
User_Defined = Doc.Name & " processed
successfully."
Err_ReEntry:
Exit Function
Err_Handler:
User_Defined = Doc.Name & " failed to process. Error summary: " &
Err.Description & "."
Resume Err_ReEntry
End Function
Function User_Defined(ByRef Doc As Word.Document) As String
On Error GoTo Err_Handler
'Change the Heading 1 style font to "Tahoma"
With Doc
.Styles("Heading 1").Font.Name = "Tahoma"
End With
User_Defined = Doc.Name & " processed successfully."
Err_ReEntry:
Exit Function
Err_Handler:
User_Defined = Doc.Name & " failed to process. Error summary: " &
Err.Description & "."
Resume Err_ReEntry
End Function |
|
That's all there is to it. Download the
AddIns here: Batch Process AddIns |
|
The project module and UserForms contained
in these two AddIns are not protected so they are available for you to
reiview or modify. The code contains techniques and methods
that you may find interesting or helpful for other projects
including: a. Use of a modeless UserForm
b. Use of a collection to save the existing recent
files list before processing begins and then restore that original
list after processing.
c. A recursive function to process one
or more folders in the batch folder
d. Code to control UserForm captions and
command button displays
e. Code to validate find and replace field
entries
f. and several others. |
|