Batch File Formatting with PowerShell

Below is an example of how paragraphs of text in Microsoft Word documents can be formatted in a batch, based on specified paragraph text.

Firstly, the file path is set, together with an array of paragraphs containing particular text to format. If the path exists, the current working directory is changed and the names of all the Word files within it, excluding those in sub-directories, are retrieved. This is followed by a check to make sure that there are files to process at the desired location. Each file is then processed one by one. For each paragraph of text within a document, a check is made to see if its text matches any item in the paragraphs to format array. If a match is found the text is formatted.

The processing of the files is wrapped in a ‘try-catch’ block to handle any errors that may arise and provide feedback on a file by file basis where formatting is not possible. Where formatting is successful, feedback is also given as to the number of paragraphs formatted in each file. A total count of files formatted is provided at the end.

# Clear the console window.
Clear-Host

# File path.
$filePath = "c:\demo"

# Array of paragraphs to format.
$parasToFormat = "Example Heading 1", "Example Heading 2", "Example Heading 3"

# Check to see if the file path exists.
if (Test-Path $filePath)
{

    # Change the current working directory.
    Set-Location $filePath

    # Retrieve the names of the Word files.
    $files = Get-ChildItem -Path *.docx

    # Check if there are any Word files.
    if ($files.Count -eq 0)
    {

        # Message stating no files to format.
        Write-Host "There are no files to format."

    }
    else
    {


        # Message stating files are being processed.
        Write-Host "Processing files...`n"

        # Word file object.
        $msWord = New-Object -ComObject Word.Application

        # Set visibility and alerts to false.
        $msWord.Visible = $false
        $msWord.Application.DisplayAlerts = $false

        # Formatted file and paragraph counts.
        $filesFormatted = 0
        $paragraphsFormatted = 0

        # Process the Word files.
        foreach ($file in $files)
        {
        
            try
            {
            
                # Open the Word document.
                $wordDoc = $msWord.Documents.Open($file.FullName)
                
                # Process the paragraphs in the document.
                foreach ($para in $wordDoc.Paragraphs)
                {
                    
                    # Check if the paragraph text is one that needs formatting.
                    if ($parasToFormat -contains $para.Range.Text.Trim())
                    {

                        # Format the paragraph font, weight and size.
                        $para.Range.Font.Name = "Arial"
                        $para.Range.Font.Bold = $TRUE
                        $para.Range.Font.Size = 14

                        # Indicate a paragraph has been formatted.
                        $paragraphsFormatted += 1

                    }

                }

                # Check if any paragraphs have been formatted.
                if ($paragraphsFormatted -gt 0)
                {

                    # Increment the files formatted count.
                    $filesFormatted += 1

                    # Save the Word document.
                    $wordDoc.Save()

                    # Message displaying file formatting information.
                    if ($paragraphsFormatted -eq 1)
                    {

                        $message = $paragraphsFormatted.ToString()
                        $message += " paragraph formatted in the file `""
                        $message += $file.FullName
                        $message += "`"."
                        Write-Host $message

                    }
                    else
                    {

                        $message = $paragraphsFormatted.ToString()
                        $message += " paragraphs formatted in the file `""
                        $message += $file.FullName
                        $message += "`"."
                        Write-Host $message

                    }

                    # Reset the paragraphs formatted variable.
                    $paragraphsFormatted = 0

                }

                # Close the Word document.
                $wordDoc.Close()

            }
            catch
            {

                # Message stating file could not be formatted.
                Write-Host "The file `"$file.FullName`" could not be formatted."

            }

        }

        # Stop any Word processes to make sure its closed.
        Stop-Process -name WINWORD

        # Message stating the number of files formatted.
        if ($filesFormatted -eq 0)
        {

            Write-Host "No files have been formatted."

        }
        elseif ($filesFormatted -eq 1)
        {

            Write-Host "$filesFormatted file has been formatted."

        }
        else
        {

            Write-Host "$filesFormatted files have been formatted."

        }

    }

}
else
{

    # Message stating file path does not exist.
    Write-Host "File path does not exist."

}