Graham Mayor

... helping to ease the lives of Microsoft Word users.

Many people access the material from this web site daily. Most just take what they want and run. That's OK, provided they are not selling on the material as their own; however if your productivity gains from the material you have used, a donation from the money you have saved would help to ensure the continued availability of this resource. Click the appropriate button above to access PayPal.

Apply processes to a batch of Word documents

Elsewhere on this web site there are batch processes to perform a number of tasks on a range of documents. The add-in featured here below is intended to cover those processes that have hitherto not been included.

The original version of the add-in was inspired by an add-in from my old friend Greg Maxey, to which it bore only a superficial resemblance; however as so often happens when we share ideas, he wanted to get involved in the further development, so from Version 2,0 the add-in is a collaborative effort.

The version you see illustrated below is functionally similar to Greg's version, though the branding reflects our individual tastes with links to resources on our own sites.

As with all the add-ins from this site, it is presented as a Word template and is supplied in zipped format complete with installation instructions. When installed it will display a button on the Add-Ins Tab of the Word ribbon as shown below (here in Word 2013):

One of the processes, pre-configured in the add-in template, employs a table stored in the body of the template itself, and which may be accessed by opening the template for editing in Word.

The table is intended to be editable, providing user-configurable find and replace pairs of words or phrases. The table header row is ignored when processing the documents.

Entries included in the table must conform to Word's find and replace rules and, where the option is selected, the use of wildcards - See the dedicated page on the subject of Using Wildcards in Word's Find and Replace dialog.   

The add-in is intended for use with Word compatible documents and templates and is error trapped against inappropriate use.

This dialog allows documents in a selected folder (and optionally its sub-folders) to be processed, with one of a range of built-in processes. Where the active document has content, the add-in also offers the option to process that document (shown unavailable in the above illustration).

 

Important! The add-in will not allow the selection of the root of any accessible drive (see below). The potential for causing havoc is too great. I had considered allowing it with strong warnings, but frankly I couldn't think of a circumstance where it was desirable to be able to process the root of a drive, so I have blocked it and the text box will change colour to reinforce the error.

The textbox colour will also indicate whether a manually entered folder is a valid folder, showing pink when the folder does not exist, changing to green when it does.

 

The processes

  1. Convert date fields
  2. Find and replace
  3. Find and replace (with table defined pairs)
  4. Convert older format documents
  5. Replace smart quotes with plain quotes
  6. Convert to PDF
  7. Convert to PDF with PDFCreator (Only when PDFCreator 2.# is available)
  8. Replace plain quotes with smart quotes
  9. Add document password protection
  10. Remove document password protection
  11. User Defined Process

The main userform dialog has context sensitive help, available by clicking the help button:

Convert date fields

This function converts { DATE } fields to { CREATEDATE } fields retaining any formatting switch that may be applied or adding a switch from a large selection of date formatting switches, when no original switch is present. The original switch thus takes precedence over the selected date format.

Where { DATE } fields are locked against updating, they are not converted.

Time fields

Historically Word has also used the { TIME } field to display the system date from the PC, therefore the 'Convert dates' function also converts { TIME } fields to { CREATEDATE } fields. However in the case of { TIME } fields, the fields are only converted where there is a date formatting switch present and when the field is not locked against updating.

In documents where time fields are used for logging times and dates and then locked to preserve those times and dates, the function should not affect those fields, however the complexity of field construction can be such that you should use test the process using a copy of a document, before committing the process to run on a batch of files, to ensure that it provides the required results.

Find and replace

The option provides a function to find and replace one word or text with another where-ever they happen to be in the documents. The process includes an option to search for whole words only and/or the use of wildcards - see Replace using wildcards.

To apply case sensitive searches, set the wildcard option.

Find and replace with table defined pairs

Whereas the previous option provides for a single pair of words or phrases, the table option enables multiple searches to be performed on the same document. The table containing the pairs of words or phrases are stored in the body of the add-in template and may be accessed by opening the template in Word.

With regard to the two cells at the right hand end of the table (illustrated above). Enter Y in the WC column to indicate the use of a wildcard search. The code uses only the first letter to indicate whether to employ a wildcard search, so 'Yes' or 'yes' would be equally valid.

If wildcard is not used, enter Y in the match whole word column 'WWO' to process whole word only.

Note: When a wildcard is used for a defined pair, match whole word is ignored.

Testing your search and replace strings in a document before committing to a batch process will save a lot of grief later.

 

Convert to PDF

Uses the PDF creation function built into Word to save a batch of documents in PDF format. This function is not available for single documents as it is easier to simply save the document from Word.

Convert to PDF using PDFCreator

This function is not available for single documents as it is easier to simply print the document from Word to the PDFCreator driver.

PDF creation using the built-in PDF Converter is much faster than creation with PDF Creator, so unless you need the extra functionality offered by PDF Creator, I would advise the use of the built-in converter.

The process has been configured to use the current version of PDF Creator and will no longer work with version 1.7.3.

Note that this also includes PDF Architect, which is not required, and unless you want this application, I would recommend that you use the 'Expert' installation settings and uncheck PDF Architect. Note also that the installer will offer assorted plug-ins for your browser, that are certainly not required. Make sure you de-select these during the installation.

When converting to PDF format with PDFCreator the add-in additionally provides the option to engage PDF security measures. When the 'Continue' button on the main dialog is clicked, a second userform will open to offer that facility. Clicking 'Cancel' on this userform will cancel the whole process.

The items shown disabled in the following illustration become available when a Master Password is entered. If you don't want additional security measures then don't set a Master Password!

Currently the additional security options are only supported when the option PDF is checked. Should you check one of the other PDF format types, the dialog changes as shown below and any security settings entered are returned to their default values.

If you enter a Master Password and continue without setting options, you will see the following dialog. Click 'No' to set options or 'Yes' to continue without.

The PDF files are created in the same folder as the documents.

Smart Quotes

The add-in provides processes to convert straight quotes to English/US pattern smart quotes and vice versa. Note that international smart quotes are not provided for.

Add document password protection

This option provides for the addition of a common password to open the processed documents, with optionally the facility to add a password to modify the documents:

This option can be used to replace an existing password. If the 'Require password' check box is checked (see next illustration) documents that are password protected may be processed, and this option will ensure that the password dialog is presented.

 

 

Remove document protection

This option provides for the removal of common passwords from the processed documents

Run a user defined process

This option for experienced users with some VBA programming knowledge, allows the running of a user configured macro.

The 'User Defined Process' option allows users with VBA experience to define and execute their own processing procedures. "The procedure must be a 'Function' of a boolean variable type, defined in a standard module located in the user's 'Normal' template.

It must include a document object variable as a single parameter, and a basic error handler.. e.g.

Function MacroName(oDoc as Document) As Boolean

or

Function MacroName(ByRef oDoc As Word.Document) As Boolean

 

A sample of the basic structure required is shown in the first macro code panel below:

Function MyUserDefinedProcess(oDoc As Document) As Boolean
On Error GoTo Err_Handler
'Do Something with oDoc here. e.g.,"
oDoc.Range.InsertAfter "Some Text"
MyUserDefinedProcess = True
lbl_Exit:
Exit Function
Err_Handler:
Select Case Err.Number
'Case Is = "Your handled errors e.g., 5109"
Case Else
MyUserDefinedProcess = False
Resume lbl_Exit
End Select
End Function

 

The macro function must not close the document, as that is handled in the add-in code.

For a real world example you may wish to use the process to replace a company logo, or to add text to the document etc. e.g. The example shown below will change an autotext field in the header (first page header only if appropriate) from "MyLogo" to "MyNewLogo":

Function ChangeLogoBuildingBlock(ByRef oDoc As Word.Document) As Boolean
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oRng As Range
Dim fld As Field
On Error GoTo Err_Handler
Set oSection = oDoc.Sections(1)
If oDoc.PageSetup.DifferentFirstPageHeaderFooter = True Then
Set oHeader = oSection.Headers(wdHeaderFooterFirstPage)
Set oRng = oHeader.Range
For Each fld In oRng.Fields
If fld.Type = wdFieldAutoText Then
If InStr(1, fld.Code, "MyLogo") > 0 Then
fld.Code.Text = Replace(fld.Code.Text, "MyLogo", "MyNewLogo")
fld.Update
Exit For
End If
End If
Next fld
Else
Set oHeader = oSection.Headers(wdHeaderFooterPrimary)
Set oRng = oHeader.Range
For Each fld In oRng.Fields
If fld.Type = wdFieldAutoText Then
If InStr(1, fld.Code, "MyLogo") > 0 Then
fld.Code.Text = Replace(fld.Code.Text, "MyLogo", "MyNewLogo")
fld.Update
Exit For
End If
End If
Next fld
End If
ChangeLogoBuildingBlock = True
lbl_Exit:
Exit Function
Err_Handler:
ChangeLogoBuildingBlock = False
Resume lbl_Exit
End Function

To run the macro from the batch process add-in, enter "ChangeLogoBuildingBlock" in the dialog macro name text box:

 

Password protection (see also the sections above on adding and removing passwords)

The add-in will accommodate documents and templates which are password protected against opening, modification, and in the case of protected forms against editing by checking the 'Require password' check box and entering the common passwords for the documents in the password dialog.

Documents for which a password is missing or incorrect are not processed but logged for later reference.

A convenient oddity in the way Word VBA opens documents allows documents without password protection to be opened when a password that is not relevant to them is applied, thus the add-in will open documents that are not protected even though you set a password here.

 

Protection for forms

 

Progress indication

Where the add-in is processing multiple documents from a folder or a folder and its sub folders, progress indicators provide information about the current status:

When all is done, any unprocessed files are logged by name, with their paths, in a document which is left of screen. This document is unsaved and can be disposed of when the information it contains has been assimilated.

 

 

- Click here to download the add-in

 

 

 Batch Processing

It is a common requirement for users to wish to apply similar information to a number of documents.

The add-in featured on this page will allow you to run a variety of processes, some built-in, others of your own design, on the currently active document, or on a folder containing documents, and, if required, any sub folders of that folder.