Bo2SS

Bo2SS

Batch Processing in Word | Awesome Word Bro, it turns out your playability is still so high!

This article will implement three capabilities for batch processing Word documents: 1) Text replacement + formatting, 2) Deleting the last page, 3) Adding content at the end. ⚠️: Remember to back up before use, remember to back up, remember to back up!

image

On a weekend morning ☀️, I found my girlfriend Emo in front of the computer 💻. I looked at her 👀, and she seemed hesitant to speak 🤭, as if she wanted me to accompany her to work overtime 😭.

But as a programmer, what could I do to help this lawyer 👩🏻‍💼?

Finally, my girlfriend spoke up 🎙️: “Our boss asked us to replace this with that in these hundreds of Word documents, change this format to this, change that format to that, and also remove the content on the last page, replacing it with something else...”

Me: Oh? This seems like a laborious task 🧱? How can I let you do it? Let me handle it!

image


Requirement Analysis#

Background: The lawyer needs to process hundreds of Word documents in a systematic way. Is the promised weekend hike about to be ruined?

image

Requirements:

  1. Batch processing: No need to manually open each Word document one by one;
  2. Text replacement: A→B;
  3. Delete the last page;
  4. Add content at the end.

Solution#

Take out the long-unused Windows computer and open Word.

Combining "third-party tools" and "self-written VBA," I successfully completed this task. ✅ ✅ ✅

Prerequisite Knowledge: VBA#

To achieve batch processing, you first need to understand VBA:

Visual Basic for Applications (VBA) is a macro language for Visual Basic, developed by Microsoft to perform common automation tasks in its desktop applications, commonly used to extend the functionality of Microsoft Office software.

We can write VBA macros based on the built-in "Developer Tools" in Word. The entry point for this developer tool is here:

image

If you do not have this tab, you need to add it through Word > File > Options > Customize Ribbon:

image

Next, we can write VBA through Developer Tools > Code section~

About the function of recording macros: If you are unsure which Visual Basic or properties to use, you can record a macro to automatically convert the operations performed manually in Word into Visual Basic code, and then slightly adjust the code to clarify the desired operations. For specific steps, refer to the official documentation — Microsoft Technical Documentation: Generating Code by Recording a Macro.

Third-party Tool: WordSR#

Wait, such a common function as "batch text replacement" must have already been implemented by someone. Sure enough, Funduc Software has developed a software called Word Search and Replace 2.40, which also has a graphical interface: (The company has also developed upgraded versions, Replace Studio Pro, Replace Studio Business Edition, and Search and Replace, but they all have garbled issues when searching Chinese documents, which I have personally tested; if interested, you can research them.)

image

Similar to Word's find and replace function, but with the ability to select multiple files.

  1. The first input box: Fill in the content to search for;

  2. The second input box: Enter the content to replace;

  3. The third input box: Enter the file path to be processed in bulk. Note that *.doc represents all .doc files in the folder to be processed, while *.doc* will also process *.docs files.

PS: It also supports regular expression matching; remember to check "Use Pattern Matching"; if documents in subfolders also need to be replaced, remember to check "Search Subdirectories."

Usage Steps:

⚠️: Remember to back up before use, remember to back up, remember to back up.

  1. Go to http://www.funduc.com/word_sr.htm, download the software package.

image

  1. After extracting, as shown below, double-click to open the WordSR_240_64bit.docm file (choose according to your system and Office version; nowadays, most computers are 64-bit systems).

image

  1. Double-click the text "Double Click Here To Run Word Search and Replace" (if Word pops up a security warning: macros have been disabled, click to enable content first), and then you can use it happily~

image

Note: The document above also contains detailed usage instructions; if you encounter something you don't understand, you can browse through it; by clicking Developer Tools > Visual Basic, you can also view its source code, which is essentially VBA.

Alright 😊, with this third-party tool, I have already helped the lawyer complete most of the replacement work, but there are still some customized needs, such as some specific text needs to be bold or underlined. How can we achieve that? Do it yourself, and you'll be self-sufficient.

Customized Functions#

The customized requirements are: 1) Text replacement with formatting; 2) Deleting the last page; 3) Adding content at the end

0) Batch Processing#

First, the most important thing to achieve these requirements is actually the ability of batch processing, which is to implement the ability to loop through folders ⭐️.

The code is as follows: (I have added some comments to the code for easier understanding and modification as needed.)

Sub LoopThroughFolder()
'
' Loop through folder macro
'
'
    On Error Resume Next 'When a "runtime error" occurs in the macro, the program will continue running without interruption

    'Dim declares variables, As specifies the type, if not specified, it defaults to Variant type
    '$ is shorthand for As String, & is shorthand for As Long
    Dim objShell As Object, objFolder As Object, pPath$, f As Object, fd As Object, fso As Object, Stack$(), top&, n&, stxt$, doc As Document, x&
    
    'Set is used to assign object references
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.BrowseForFolder(0, "Please select the folder for batch processing!", 0, 0)
    If (objFolder Is Nothing) Then Exit Sub  'If Cancel is selected, exit directly
    
    'Get the folder path
    pPath = objFolder.self.Path & ""

    Set objShell = Nothing
    Set objFolder = Nothing

    Set fso = CreateObject("Scripting.FileSystemObject")

    top = 1
    ReDim Stack(0 To top)

    'Traverse the folder, including subfolders (using a stack structure to record)
    Do While top >= 1
        For Each f In fso.GetFolder(pPath).Files
            n = n + 1
            stxt = f.Path
            'Process .doc and .docs files
                If stxt Like "*.doc*" Then
                Set doc = Documents.Open(FileName:=stxt, Visible:=True)  'Visible set to True, some operations need to be visible to take effect
                Call TextReplacement  '【Processing method for a single document】Please select any macro to use!
                doc.Close SaveChanges:=wdSaveChanges  ':= used to assign values to parameters
                x = x + 1
            End If
        Next
        For Each fd In fso.GetFolder(pPath).SubFolders
            Stack(top) = fd.Path
            top = top + 1
            If top > UBound(Stack) Then ReDim Preserve Stack(0 To top)
        Next
        If top > 0 Then pPath = Stack(top - 1): top = top - 1  '":" used to connect two statements
    Loop

    Set f = Nothing
    Set fd = Nothing
    Set fso = Nothing

    'If the return value of the function is not used, parentheses are not needed; otherwise, parameters need to be enclosed in parentheses
    MsgBox "The folder contains " & n & " files!" & vbCr & "A total of " & x & " Word documents (* .docx / *.doc) have been processed!", 0 + 48
    
End Sub

This code implements traversing a specific folder and performing the same processing on each file, which depends on the Call TextReplacement line of code.

And TextReplacement is a macro written below, which can also be understood as a function.

1) Text Replacement with Formatting#

The code is as follows: (I have also written comments, and you can modify the format as needed)

Sub TextReplacement()
'
' Text replacement macro
'
'
    With ActiveDocument.Content.Find
        .ClearFormatting
        With .Replacement
            .ClearFormatting
            .Font.Bold = True  'Bold font
            .Font.Underline = wdUnderlineSingle  'Add single underline
        End With
    
        'Execute find and replace: 'FindText is the text to find, ReplaceWith is the text to replace, Format is True for formatted
        '"_" is used for line breaks
        .Execute _
            FindText:="公众号", _
            ReplaceWith:="Bo2SS", _
            Format:=True, _
            Replace:=wdReplaceAll  'wdReplaceAll replaces all, wdReplaceOne replaces once
    End With
    
End Sub

2) Deleting the Last Page#

The code is as follows: (See comments for details)

Sub DeleteLastPage()
'
' Delete last page macro
'
'
    Dim pageNum As Long, StartPage As Long, EndPage As Long
    
    'Get total number of pages
    pageNum = Word.ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
    
    'Determine the starting range of the last page and select it
    StartPage = Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=pageNum - 1).Start
    EndPage = ActiveDocument.Content.End
    ActiveDocument.Range(StartPage, EndPage).Select
    
    'Delete selected content
    Selection.Delete
    
End Sub

In the batch processing code, replace Call TextReplacement with Call DeleteLastPage to use it.

3) Adding Content at the End#

The code is as follows: (See comments for details)

Sub AddContentAtEnd()
'
' Add content at the end macro
'
'
   Dim oRng As Range, oDoc As Document
   
   Set oDoc = Word.ActiveDocument
   Set oRng = oDoc.Content
   
   With oRng
        'Insert characters at the end of the document, such as Chr(10) adds a line break, "Hello" adds Hello
        .InsertAfter Chr(10)
        '.InsertAfter "Hello"
   End With
End Sub

In the batch processing code, replace Call TextReplacement with Call AddContentAtEnd to use it.

🔎 Specific Usage Steps#

⚠️: Remember to back up before use, remember to back up, remember to back up.

a) Select Developer Tools > Click Macros > Enter macro name > Click Create;

image

b) In the opened VBA editor, copy the above 4 segments of code;

image

c) Move the cursor to the "LoopThroughFolder" macro, press F5 to run, a folder selection window will pop up, and after selecting the folder, each file inside will be processed accordingly!

image

Tips#

  • Force stop macro execution: Ctrl + break.
  • When you have some unclear usages of VBA, first go to Microsoft Technical Documentation: Word VBA Reference to find answers!
  • There is a plugin in VScode called VSCode VBA, which makes writing VBA more visually appealing~

So, did you really feel the playability of Word Bro?

Microsoft, Respect!


By now, my girlfriend had also completed her task early, and we happily went hiking together. The cover of this post is also one of the rewards for this task 👀~

image

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.