Ejemplo n.º 1
0
        /// <summary>
        /// A function that merges Microsoft Word Documents that uses a template specified by the user
        /// </summary>
        /// <param name="filesToMerge">An array of files that we want to merge</param>
        /// <param name="outputFilename">The filename of the merged document</param>
        /// <param name="insertPageBreaks">Set to true if you want to have page breaks inserted after each document</param>
        /// <param name="documentTemplate">The word document you want to use to serve as the template</param>
        private void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
        {
            object defaultTemplate = documentTemplate;
            object missing         = System.Type.Missing;
            object pageBreak       = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
            object outputFile      = outputFilename;

            // Create  a new Word application
            Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();
            if (filesToMerge.Count() == 1)
            {
                pageBreak = false;
            }
            try
            {
                // Create a new file based on our template
                Microsoft.Office.Interop.Word._Document wordDocument = wordApplication.Documents.Add(ref defaultTemplate, ref missing, ref missing, ref missing);

                // Make a Word selection object.
                Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;

                int index = 0;

                // Loop thru each of the Word documents
                foreach (string file in filesToMerge)
                {
                    // Insert the files to our template
                    selection.InsertFile(file, ref missing, ref missing, ref missing, ref missing);

                    //Do we want page breaks added after each documents?
                    if (insertPageBreaks && index != filesToMerge.Count() - 1)
                    {
                        selection.InsertBreak(ref pageBreak);
                    }

                    index++;
                }

                // 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.Close(ref missing, ref missing, ref missing);
                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);
            }
        }
Ejemplo n.º 2
0
        public void Merge() // Shall be called on a separate thread and shall make use of the member variables
        {
            object missing    = System.Type.Missing;
            object pageBreak  = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
            object outputFile = outputFilePath;

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

            bSuccessfulOperation = true;
            try
            {
                // Create a new file
                Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents.Add(
                    ref missing
                    , ref missing
                    , ref missing
                    , ref missing);

                // Make a Word selection object.
                Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;

                //Count the number of documents to insert;
                int documentCount = fileList.Length;

                //A counter that signals that we shoudn't insert a page break at the end of document.
                int breakStop = 0;

                int iCounter   = 0;
                int iFileCount = fileList.Length;

                // Iterate through the entire list of files
                foreach (string file in fileList)
                {
                    Console.WriteLine("Merging file: " + file);
                    breakStop++;
                    // Insert the files to our template
                    selection.InsertFile(
                        file
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing);

                    //Do we want page breaks added after each documents?
                    if (insertPageBreaks && breakStop != documentCount)
                    {
                        selection.InsertBreak(ref pageBreak);
                    }
                    iCounter++;
                    UpdateProgressBar(((float)iCounter / iFileCount) * 100);
                }

                // 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);

                // Get some stats about the merged file
                lineCount = wordDocument.Sentences.Count;
                wordCount = wordDocument.Words.Count;

                Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
                pageCount = wordDocument.ComputeStatistics(stat, ref missing);
                wordDocument.Save();

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


            DisplayResult(); // Done, show the results
        }