Batch File Formatting with C#

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 a list of paragraphs containing particular text to format. A check is then made to see if the file path exists and whether there are any files to format. Each file is then processed one by one. All files without a ‘.docx’ extension are ignored. 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 list. If a match is found the text is formatted.

Feedback is given as to the number of paragraphs that have been formatted in each file, along with a total count of files formatted at the end.

using System;
using System.Collections.Generic;
using System.IO;
using Word = Microsoft.Office.Interop.Word;


namespace BatchFileFormatting
{
    class Program
    {
        static void Main(string[] args)
        {

            // File path.
            string filePath = @"C:\demo\";

            // List of paragraphs to format.
            List parasToFormat = new List() {
                "Example Heading 1",
                "Example Heading 2",
                "Example Heading 3"
            };

            // File extension.
            string fileExtension;

            // Check to see if the file path exists.
            if (Directory.Exists(filePath))
            {

                // Return the names of the files at the specified path.
                string[] dirFiles = Directory.GetFiles(filePath);

                // Check if there are any files at the path.
                if (dirFiles.Length == 0)
                {

                    // Message stating there are no files to format.
                    Console.WriteLine("There are no files to format.");

                }
                else
                {

                    // Microsoft Word file variables.
                    Word.Application wdApp = new Word.Application();
                    Word.Document wdDocument;

                    // Formatted file and paragraph counts.
                    int filesFormatted = 0;
                    int paragraphsFormatted = 0;

                    // Process the files at the path.
                    foreach (string dirFile in dirFiles)
                    {

                        // Extract the file extension from the name.
                        fileExtension = Path.GetExtension(dirFile);

                        // Check if the file is a Word file.
                        if (fileExtension == ".docx" && !dirFile.Contains("~"))
                        {

                            try
                            {
                                
                                // Open the Word document.
                                wdDocument = wdApp.Documents.Open(@dirFile);

                                // Process the paragraphs in the document.
                                foreach (Word.Paragraph para in wdDocument.Paragraphs)
                                {

                                    // Check if the paragraph text is one that needs formatting.
                                    if (parasToFormat.Contains(para.Range.Text.ToString().Trim()))
                                    {

                                        // Format the paragraph font, weight and size.
                                        para.Range.Font.Name = "Arial";
                                        para.Range.Font.Bold = -1;
                                        para.Range.Font.Size = 14;

                                        // Indicate a paragraph has been formatted.
                                        paragraphsFormatted += 1;

                                    }

                                }

                                // Check if any paragraphs have been formatted.
                                if (paragraphsFormatted > 0)
                                {

                                    // Increment the files formatted count.
                                    filesFormatted += 1;

                                    // Save the document.
                                    wdDocument.Save();

                                    // Message displaying file formatting information.
                                    if (paragraphsFormatted == 1)
                                    {

                                        Console.WriteLine("{0} paragraph formatted in the file " +
                                            "\"{1}\".", paragraphsFormatted, dirFile.ToString());

                                    }
                                    else
                                    {

                                        Console.WriteLine("{0} paragraphs formatted in the file" +
                                            " \"{1}\".", paragraphsFormatted, dirFile.ToString());

                                    }

                                    // Reset the paragraphs formatted variable.
                                    paragraphsFormatted = 0;

                                }

                                // Close the Word document.
                                wdDocument.Close();

                            }
                            catch (Exception e)
                            {

                                // Message confirming the file could not be formatted.
                                Console.WriteLine("The file \"{0}\" could not be formatted.",
                                    dirFile.ToString());

                            }

                        }

                    }

                    // Message stating the number of files formatted.
                    if (filesFormatted == 0)
                    {
                        Console.WriteLine("No files have been formatted.");
                    }
                    else if (filesFormatted == 1)
                    {
                        Console.WriteLine("{0} file has been formatted.", filesFormatted);
                    }
                    else
                    {
                        Console.WriteLine("{0} files have been formatted.", filesFormatted);
                    }

                    // Close Microsoft Word.
                    wdApp.Quit();

                }

            }
            else
            {

                // Display a message stating file path does not exist.
                Console.WriteLine("File path does not exist.");

            }

            // Force console window to stay open until a key is pressed.
            Console.ReadKey();

        }
    }
}

Further Resources