简体   繁体   中英

Merge and Split Word Documents using Microsoft.Office.Interop.Word in C#

I need to merge many Word files into a single file, send it for a revisor and split it again to same separated files. There is about 200 small Word documents.

So, when I'm doing the merge I need to add any type of mark for reference for when I will split it. I'm actually adding a tag with the original file name, the final Word file will be like this:

[ c:\\doc\\file1.doc ]

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis eget ipsum non est ultricies bibendum ac a sapien. Etiam facilisis nunc ut arcu tincidunt, in fermentum ipsum pretium. Phasellus non viverra orci. Vestibulum varius vulputate leo quis fermentum. Phasellus adipiscing diam ultricies odio accumsan, et dapibus velit dapibus. Sed eleifend lectus et lacinia facilisis. Pellentesque eleifend, purus in convallis faucibus, sapien purus fringilla arcu, a volutpat dolor arcu ullamcorper purus. In viverra magna neque, eget imperdiet urna luctus at. In hac habitasse platea dictumst. Praesent aliquam arcu diam, quis fermentum lacus pellentesque ut. Aliquam nulla eros, porttitor quis molestie eu, mollis vel lacus. Sed nec aliquam libero. Donec vel congue sapien, sed dignissim nisl. Praesent dui nulla, fringilla iaculis lorem id, lacinia imperdiet odio.

[ c:\\doc\\file1.doc ]

[ c:\\doc\\file2.doc ]

Proin eu consectetur turpis, vel sagittis arcu. Mauris iaculis lacus ut orci adipiscing, vitae eleifend ipsum egestas. Suspendisse ullamcorper consequat laoreet. Nullam interdum augue eget ante tempor porttitor. Sed dignissim nulla libero, eu ultricies urna vestibulum quis. Phasellus rhoncus leo sed leo gravida, nec ullamcorper neque tempor. Sed sollicitudin, nisi ut lobortis sollicitudin, dui enim tristique leo, ac sodales leo elit quis odio. Nulla dictum mattis mi in tempus.

[ c:\\doc\\file2.doc ]

I'm using this code to merge the files, working fine:

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

namespace MyDocs
{
  public class MsWord
  {
    public static void Merge(List<string> filesToMerge, string outputFilename, string documentTemplate)
    {
      object defaultTemplate = documentTemplate;
      object missing = System.Type.Missing;
      object outputFile = outputFilename;

      // Create  a new Word application
      Word._Application wordApplication = new Word.Application();

      try
      {
        // Create a new file based on our template
        Word._Document wordDocument = wordApplication.Documents.Add(ref defaultTemplate, ref missing, ref missing, ref missing);

        // Make a Word selection object.
        Word.Selection selection = wordApplication.Selection;

        // Loop thru each of the Word documents
        foreach(var file in filesToMerge)
        {
            // create a tag with the file name
            string uid = String.Format("\n[ {0} ]\n", file);

            selection.TypeText(uid);
            selection.InsertFile(file, ref missing, ref missing, ref missing, ref missing);
            selection.TypeText(uid);
        }

        // Save the document to it's output file.
        wordDocument.SaveAs(ref outputFile, 
                            ref missing, ref missing, ref missing, ref missing, ref missing, 
                            ref missing, ref missing, ref missing, ref missing, ref missing, 
                            ref missing, ref missing, ref missing, ref missing, ref missing);

        // Clean up!
        wordDocument = null;
      }
      catch (Exception ex)
      {
        //I didn't include a default error handler so i'm just throwing the error
        throw ex;
      }
      finally
      {
        // Finally, Close our Word application
        wordApplication.Quit(ref missing, ref missing, ref missing);
      }
    }
  }
}

Now I'm stucked, I don't now how to split and I don't understood the Interop class, I need to read the entire Word, find the tags and split it into separated files.

I think that the tag is not the best way, because I don't need show it. I tried using the Section object like this:

foreach(var file in filesToMerge)
{
    selection.Sections.Add();
    selection.InsertFile(Environment.CurrentDirectory + @"\" + file, ref missing, ref missing, ref missing, ref missing);
}

And after reading the document like this:

foreach (Word.Section section in wordDocument.Sections)
{
    // do save stuff                    
}

But now there is only 2 sections returned :(

In my opinion best options (instead of tags) would be to use bookmarks. Bookmarks are:

  1. easy to add!! something like Activedocument.bookmarks.add... (based on VBA syntax)
  2. easy to find (by name),
  3. they could be iterated by for each loop where iteration goes by bookmark name ,
  4. they have range object property which allows you to find exact point within your document where bookmark is located,
  5. they could be zero length range if needed
  6. they could be invisible if name starts with _ (underline mark, works rather only when bookmark is added programmatically)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM