Macro Batch of Word documents 4.1

Thứ tư - 06/01/2021 17:26
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.

tai ngay 1

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 add a tab and a pair of buttons to the Word ribbon as shown below :

One of the processes, pre-configured in the add-in template, employs a table stored in the body of the document 'Find and Replace Pairs Table.docx' included in the zip file, and which may be accessed by opening the template for editing in Word. This document is normally stored in the user's documents folder,

Tracking changes would adversely affect some of the processes. Where tracking changes may be an issue, tracking is turned off while processing then restored to its original setting.

Versions of the add-in before version 3.9, employed a table in the add-in itself to enable the find and replace option using table pairs. It was felt that Microsoft's ever more intrusive security may prevent this table from being edited and saved by some users, and so with version 3.9, the table was provided in a separate document.

The document 'Find and Replace Pairs Table.docx' would normally be filed in the user's Documents folder, where the add-in will find it if present. If preferred it can be stored in any trusted folder that has read/write access, but then the first time that option is used, the add-in will prompt for its location (see illustration below).

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. It may not work with later versions of PDF Creator)
  8. Replace plain quotes with smart quotes
  9. Add document password protection
  10. Remove document password protection
  11. Remove personal information
  12. 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 a document 'Find and Replace Pairs Table.docx' supplied with the add-in and may be accessed by opening the document in Word.

You may add to or change any of the entries below the header row of the table. Do not change the layout of the table or the filename of the document. The document would normally be saved in the user's Documents folder, where it will be found automatically. File it anywhere else and you will be prompted for the location. Save the document after making any changes to the table and before using it in conjunction with the process.

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

Remove personal information

This option allows users to remove personal information from the document's metadata before publishing the document. The process also optionally offers the options to 'Accept All Tracked Changes' and to 'Remove Comments'. When the 'Remove Comments' check box is left unchecked, the comments are anonymised.

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


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
Exit Function
Select Case Err.Number
'Case Is = "Your handled errors e.g., 5109"
Case Else
MyUserDefinedProcess = False
Resume lbl_Exit
End Select
End Function

Tổng số điểm của bài viết là: 0 trong 0 đánh giá

Click để đánh giá bài viết

  Ý kiến bạn đọc

Giới thiệu về Website

Website cá nhân cung cấp thông tin hỗ trợ giáo viên trung học tỉnh Đồng Tháp thực hiện chương trình giáo dục phổ thông 2018

Đăng nhập hệ thống LMS
Thăm dò ý kiến

Bạn có tìm được thông tin hữu ích?

Thống kê
  • Đang truy cập6
  • Máy chủ tìm kiếm4
  • Khách viếng thăm2
  • Hôm nay561
  • Tháng hiện tại6,736
  • Tổng lượt truy cập527,206
Đăng nhập
Hãy đăng nhập thành viên để trải nghiệm đầy đủ các tiện ích trên site
Bạn đã không sử dụng Site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây