Find and Replace in Word Documents
Operator: Abdulla Al Fahad. How tools are built and checked
A supplier changes its trading name, a client is acquired, a product loses its old trademark, or a year ticks over: suddenly a folder of contracts, letters and templates all carry wording that has to go. Word's own Find and Replace box works on one open document and one term at a time, so twelve agreements and five terms mean sixty rounds of typing, saving and hoping none was missed. This tool takes the whole list of pairs at once and runs it over as many as 50 .docx files, then tells you how many replacements each file received before you download anything.
The part that usually breaks scripted replacements is how Word stores text. A paragraph is cut into runs, and a new run starts wherever formatting changes or an edit, spell check or tracked revision happened. A name typed once and later bolded halfway can sit in the file as "Ac" in one run and "me Ltd" in the next, and a search that looks at one run at a time walks straight past it. Here each paragraph is searched as the reader sees it, with all its runs joined, and the replacement is written back into the run where the match begins, keeping that run's font, size and colour.
Key features
- One list of find and replace pairs applied to up to 50 .docx files in a single pass
- Finds text that Word has split across differently formatted runs inside a paragraph
- The replacement keeps the formatting of the run where the match begins
- Match case and whole-word options, both on by default
- Choice of parts: body, headers and footers, footnotes and endnotes, and comments (off by default)
- Paragraphs with tracked insertions, deletions or moves are left alone and counted as skipped
- Rows typed one by one, or a two-column list pasted as CSV or straight from a spreadsheet
- Preview with counts per file, per part and per row before any download
- CSV change log with the text before and after every replacement
How edited files are packaged and logged
Input is Word documents saved as .docx (Office Open XML), whether they came from Word, LibreOffice Writer or a cloud editor's .docx export. Older binary .doc files, macro-enabled .docm files, .dotx templates and password-protected documents are refused with a note saying how to convert them. A batch holds up to 50 files, each up to 25 MB and 200 MB in total, and the list can have up to 500 rows.
Each edited document is the original package with only the changed XML parts rewritten: pictures, styles, numbering, fonts, document properties and every untouched header or footer are carried over byte for byte. One changed file downloads as yourname-replaced.docx. Several are packed into word-find-replace-N-files.zip under their original names, with " (2)" added when two files share a name, so they can be dropped back into the folder they came from. Files with no matches are left out and marked as such in the preview.
The change log is a CSV with the columns file, part, paragraph, action, find, replace_with, before and after. Before and after show up to 30 characters either side of each match, so the log can be checked without opening a single document. Paragraph numbers count every paragraph of that part from the top, empty ones and those inside tables and text boxes included. The file is UTF-8 with a byte-order mark and CRLF line ends, a cell that would start with =, +, - or @ gets a leading apostrophe so a spreadsheet will not run it as a formula, and the log stops at 20,000 rows while the on-page counts keep going.
How it works
When files are added, each one's name, size and first four bytes are checked; a .docx must begin with PK, the start of every ZIP package. Pressing Preview changes opens each package with JSZip and refuses one without word/document.xml. The relationships file of the main document then names the header, footer, footnote, endnote and comment parts, and any of those that sit in the package under their usual names are added too.
Each chosen part is read tag by tag. For every paragraph the text of its w:t elements is joined in order, while tabs, line breaks, pictures, field codes, footnote marks, symbols and content-control edges are kept as walls a match cannot cross. A paragraph that contains or sits inside a tracked insertion, deletion or move is flagged, and a paragraph inside a text box keeps its own text instead of merging with the paragraph that anchors the box.
Every row is searched as literal text, with case folding when Match case is off and a check of the characters on either side when Whole words only is on. Where matches compete for the same characters, the one that starts first wins, then the longer one, then the row higher in the list. All matches are found on the original text before anything is written, so a replacement is never searched again and one row cannot feed another.
To write a match, the run holding its first character gets the replacement in place of the matched characters, and the matched characters are removed from the runs after it. When the new text begins or ends with a space, xml:space="preserve" is added so Word keeps that space; ampersands and angle brackets are escaped. Text boxes are usually stored twice, once for current Word and once as a fallback drawing for older versions; both copies are edited so they stay identical, but only one is counted.
Download changed documents runs the same list again on each file that had changes and re-zips only the rewritten parts. The ZIP of several documents stores them without recompressing, since a .docx is already compressed.
Common use cases
- Swapping a company's old trading name for its new one across a folder of customer contracts
- Updating a year, a price list name or a policy number in a set of standing letters
- Filling placeholders such as CLIENT_NAME and START_DATE in copies of a template, one batch per client
- Replacing a former colleague's name and phone number in every procedure document they used to own
- Changing a product name in the user guides, release notes and quotes that mention it
How to use this tool
- Add your .docx files by choosing them or dropping them on the box; add more in several goes if they live in different folders. Press Try a sample letter to see the tool work on a built-in supply agreement first.
- Type each pair into the rows, or open Paste a two-column list and paste a CSV or two columns copied from a spreadsheet, then press Replace the rows with this list.
- Read any amber notes under the rows: repeated search text, a row with nothing to find, or a search term that starts or ends with a space.
- Leave Match case and Whole words only on unless you have a reason not to, and tick the parts to search.
- Press Preview changes. Check the total per file, the per-row table (a row showing "none found" usually has a typo) and the first log rows.
- Press Download changed documents, and Download change log (CSV) if you want a record of what changed and which paragraphs were skipped.
- Open one or two edited files in Word and look at the places the log lists before sending anything on.
Example
sample-supply-agreement.docx with the list Acme → Apex and Riverside Florists → Riverside Flowers & Plants (match case and whole words on)
Header: Acme Ltd · supply agreement · draft 3
1. [Ac in bold][me Ltd will supply cut flowers to Riverside Florists every Monday and Thursday.]
2. Orders placed by Riverside Florists before 14:00 are packed the same day. Send them to orders@acme.example.
3. (tracked insertion) Acme may move a delivery day after giving two weeks of notice.
4. Signed for Acme [tab] Signed for Riverside FloristsPreview: 6 replacements in 1 of 1 documents (body 5, headers and footers 1, skipped 1)
Header: Apex Ltd · supply agreement · draft 3
1. [Apex in bold][ Ltd will supply cut flowers to Riverside Flowers & Plants every Monday and Thursday.]
2. Orders placed by Riverside Flowers & Plants before 14:00 are packed the same day. Send them to orders@acme.example.
3. Unchanged; logged as "skipped: tracked change".
4. Signed for Apex [tab] Signed for Riverside Flowers & Plants
Row 1 (Acme): 3 replaced Row 2 (Riverside Florists): 3 replaced
The lower-case "acme" in the e-mail address stays because Match case is on.Tips for best results
- Run one test document first, read its log, then add the rest of the batch.
- Put the longer, more specific term in the list as well as the short one, such as "Acme Ltd" next to "Acme"; the longer match wins wherever both fit.
- If a row shows "none found", copy the term out of the document itself and paste it into the Find box; curly apostrophes and non-breaking spaces look identical to the typed ones.
- For template placeholders, use unusual tokens such as {{CLIENT}} or CLIENT_NAME so they cannot match ordinary words.
- Keep the change log with the edited files; it records the exact wording before every change.
Common pitfalls
Word turns straight quotes into curly ones as you type, so a row searching for don't with a straight apostrophe will not find don’t in the document. Non-breaking spaces, which Word's AutoFormat adds in some languages and which people type to keep a name on one line, look like ordinary spaces but do not match them either.
With Match case off, the replacement is written exactly as typed. "ACME LTD" in a capitalised heading becomes "Apex Ltd", and the heading's letter case changes with it unless the heading style sets all caps.
Text produced by a field, such as a date, a cross-reference, a mail-merge field or a document property, can be replaced where it is shown, but Word recalculates fields when they are updated or the document is printed with field updating on, and the old value returns. Change the source (the property, the bookmark, the data file) instead.
Only the visible text of a hyperlink changes. If the old company name is part of the web address behind the link, the link still points there.
Text inside charts, SmartArt and diagrams, watermarks drawn as WordArt in the header, and the title or author stored in the document properties are not part of the paragraphs searched here and stay as they are.
Edits are written as ordinary text, not as tracked changes, and they cannot be undone from Word's history. Keep the original files until you have checked the new ones.
Common errors and how to fix them
- "... does not begin with the PK signature every .docx package starts with, so it is not a Word document saved as .docx."
- The file was renamed to .docx without being converted, or it is a different format altogether. Re-save it from the program that made it as a Word Document (.docx) and add the re-saved copy to the batch.
- "... is stored in the legacy container that .doc files and password-protected Word documents use."
- Both are stored in the same legacy container, which this tool cannot open. Remove the password in Word (File, Info, Protect Document) or save the .doc as .docx, and add that file instead.
- "... is a ZIP package but holds no word/document.xml, the part every Word document keeps its text in."
- An Excel workbook, PowerPoint deck or plain ZIP archive was given a .docx name. Only Word documents can be edited here.
- "... has a damaged ZIP structure, so its parts could not be unpacked."
- The download or copy was cut short, or the file was damaged on disk. Word can often still open it; save a fresh copy from Word and add that.
- A file is refused for being over 25 MB, or the batch would pass 200 MB.
- Large .docx files are nearly always large because of pictures. Compress the pictures in Word or split the batch into two runs.
- The preview shows paragraphs as skipped.
- Those paragraphs carry tracked changes, and editing them would mix your replacement into someone's pending revision. Accept or reject the changes in Word, save, and run the file again.
Best practices
- Work on copies and keep the originals until the edited files have been opened and checked.
- Accept or reject tracked changes before a bulk replacement, so no paragraph is skipped.
- Compare the per-row totals with what you expected; a large surprise usually means a short term is matching more than intended.
- Turn Whole words only off only for terms that really should match inside longer words.
- Leave comments off unless the old name appears in reviewers' remarks that you are sending outside the team.
Why a word can be split across runs
Word saves a paragraph as a sequence of runs, each with one set of character formatting. Bold on the first two letters of a name gives two runs. So can an autocorrection, a word typed in two sittings, a spelling or grammar mark, or a language tag that changed halfway through, even when nothing looks different on screen. Two runs with identical formatting are often left unmerged.
That is why this tool joins the runs of a paragraph before searching and only then decides which runs to rewrite. The first run of the match takes the replacement, so "Ac" in bold followed by "me Ltd" in plain text becomes "Apex" in bold followed by " Ltd" in plain text, exactly as if you had selected the word in Word and typed over it. Runs between the first and last are emptied rather than deleted, which leaves the paragraph's structure and any bookmarks or comment anchors in place.
What happens to tracked changes
A paragraph that holds a tracked insertion, deletion or move belongs partly to a revision that has not been accepted yet. Rewriting its text would either change words inside someone's pending edit or leave the revision marks pointing at text that is no longer there, so the tool does neither. It still searches those paragraphs, counts each one that contains a search term, and lists them in the change log with the surrounding text, so you know exactly where to look once the revisions are settled.
A paragraph whose own paragraph mark was inserted or deleted with tracking on is treated the same way, even if its words are not marked.
How competing rows are settled
With a list of several rows, two of them can want the same characters. The rule is fixed: the match that starts earliest wins; at the same starting point the longer one wins; if they are the same length, the row higher in the list wins. Rows never chain, so a list that turns "Acme" into "Acme Group" and "Group" into "Holdings" changes "Acme" to "Acme Group" and leaves that new "Group" alone, while an existing "Group" elsewhere in the text still becomes "Holdings".
Two rows with the same search text are flagged under the list; the first is used and the second ignored, which keeps the result the same no matter how the list was pasted.
Frequently asked questions
Does the replacement keep the original formatting?
It takes the formatting of the run where the match starts. If the matched word was half bold, half plain, the whole replacement comes out bold, because that is how the first run was formatted.
Can I use wildcards or regular expressions?
No. Every row is matched as literal text, with the case and whole-word options the only variations. That keeps a list of names or terms from doing anything unexpected in a contract.
Will Word show the replacements as tracked changes?
No. The new text is written as ordinary text. If you need a redline for a reviewer, compare the original and edited documents in Word afterwards.
Can a replacement add a line break or a new paragraph?
No. Line breaks and tabs typed or pasted into a row become single spaces, and a match never reaches across a paragraph end, a tab or a line break.
Does the document's last-modified date change?
The dates stored in the document properties are not touched, because docProps/core.xml is not rewritten. Your computer will give the downloaded file its own date when it is saved.
Where are the documents processed?
In this browser tab. JSZip unpacks and repacks each document in the page's memory, and the result reaches your downloads folder through an object URL the page lets go of one second later. They are not uploaded, and the page holds the batch only while it stays open, so after a reload the files have to be added again.