none
How to copy a word document content to a new word document by c# ? RRS feed

  • Question

  •  Hi All,
       
        I am currently need to copy a existing, opened word document content to a new opened word document by C#. I am stuck at copy and paste steps. Does anyone can give some advise?
        My currently code is like this:

        private void button1_Click_3(object sender, EventArgs e)
            {
                object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

                //Start Word and create a new document.
                Word.Application oWord = new Word.Application();
               
                
                Word.Document oDoc = new Word.Document();
                oWord = new Word.Application();
                oWord.Visible = true;
                object oTemplate = Globals.ThisAddIn.Application.ActiveDocument.Path;
                oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
                    ref oMissing, ref oMissing);
                
                oDoc.ActiveWindow.Selection.WholeStory();
                
                oDoc.ActiveWindow.Selection.Copy();

                Globals.ThisAddIn.Application.ActiveDocument.Select();

               oDoc.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
               
               this.close();

    }

    Very urgent, took me the whole weekend already. Really appreciate!!!


     
    Sunday, January 11, 2009 11:48 PM

Answers

  • pkminicooper,

    Have you thought about simply copying the entire file?  If you're not doing anything else to the file, or if the first thing you want to do is copy everything, just use File.Copy instead, and then alter the file using interop after that.  There may not be any need to go into Word Interop at all here.
    David Morton - http://blog.davemorton.net/
    Monday, January 12, 2009 4:28 PM
    Moderator
  • This may not be 100% correct, but it appears to work.

    // Console application to copy the contents of an existing Word document  
    // to a new document. Requires a reference to the Microsoft Office 11.0  
    // Object Library (or similar).  
     
    using System;  
    using Word = Microsoft.Office.Interop.Word;  
     
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // Initialize a bogus parameter for arguments we don't care about  
            object oMissing = System.Reflection.Missing.Value;  
     
            // Set the paths for the old and new documents (edit as needed)  
            object oldDocPath = (object)@"c:\test.doc";  
            object newDocPath = (object)@"c:\test2.doc";  
     
            // Get a Word application and show it
            Word.Application app = new Word.Application();
     
            // Open the existing document  
            Word.Document oldDoc = app.Documents.Open(ref oldDocPath,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
     
            // Select entire contents, and copy to clipboard  
            oldDoc.ActiveWindow.Selection.WholeStory();  
            oldDoc.ActiveWindow.Selection.Copy();  
     
            // Create new document and paste from clipboard  
            Word.Document newDoc = app.Documents.Add(ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing);  
            newDoc.ActiveWindow.Selection.Paste();  
     
            // Save new document  
            app.ActiveDocument.SaveAs(ref newDocPath,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
     
            // Close the documents and quit the application  
            oldDoc.Close(ref oMissing, ref oMissing, ref oMissing);  
            newDoc.Close(ref oMissing, ref oMissing, ref oMissing);  
            app.Quit(ref oMissing, ref oMissing, ref oMissing);  
        }  
    • Proposed as answer by Harry Zhu Thursday, January 15, 2009 1:25 AM
    • Marked as answer by Harry Zhu Monday, January 19, 2009 7:52 AM
    Monday, January 12, 2009 3:46 AM
  • Or more simply:

    // Console application to save a copy of an existing Word document.  
    // Requires a reference to the Microsoft Office 11.0 Object Library (or similar).     
     
    using System;  
    using Word = Microsoft.Office.Interop.Word;  
     
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // Initialize a bogus parameter for arguments we don't care about     
            object oMissing = System.Reflection.Missing.Value;  
     
            // Set the paths for the old and new documents (edit as needed)     
            object oldDocPath = (object)@"c:\test.doc";  
            object newDocPath = (object)@"c:\test2.doc";  
     
            // Get a Word application     
            Word.Application app = new Word.Application();  
     
            // Open the existing document     
            Word.Document oldDoc = app.Documents.Open(ref oldDocPath,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
     
            // SaveAs  
            app.ActiveDocument.SaveAs(ref newDocPath,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
     
            // Close the document and quit the application     
            oldDoc.Close(ref oMissing, ref oMissing, ref oMissing);  
            app.Quit(ref oMissing, ref oMissing, ref oMissing);  
        }  
    }  
     
    • Proposed as answer by Harry Zhu Thursday, January 15, 2009 1:25 AM
    • Marked as answer by Harry Zhu Monday, January 19, 2009 7:52 AM
    Monday, January 12, 2009 3:51 AM
  • Yes, David's right. Unless you have some other task in mind which requires interop, File.Copy would be a much more direct solution. If the file has to be opened after copying, Process.Start would do it.

    With some basic exception handling to prevent the program from crashing, it might go like this:

    // Copy a file and open it using the default application.  
     
    using System;  
    using System.Diagnostics;  
    using System.IO;  
     
    class Program  
    {  
        static void Main()  
        {  
            string sourceFileName = @"c:\test.doc";  
            string destFileName = @"c:\testCopy.doc";  
            bool overwrite = true;  
     
            bool success = false;  
     
            if (File.Exists(sourceFileName))  
            {  
                try 
                {  
                    File.Copy(sourceFileName, destFileName, overwrite);  
                    success = true;  
                }  
                catch (IOException ex)  
                {  
                    Console.WriteLine("IO Exception: {0}", ex.Message);  
                }  
            }
            else
            {
                Console.WriteLine("File does not exist: {0}", sourceFileName);              
            } 
     
            if (success)  
            {  
                try 
                {  
                    Process.Start(destFileName);  
                }  
                catch (Exception ex)  
                {  
                    Console.WriteLine("Could not start process: {0}", ex.Message);  
                }  
            }  
        }  
    }  
     
    • Edited by Matt Fisher Tuesday, January 13, 2009 4:47 AM Corrected code errors
    • Proposed as answer by Ernst KuschkeModerator Tuesday, January 13, 2009 7:46 AM
    • Marked as answer by Harry Zhu Monday, January 19, 2009 7:52 AM
    Tuesday, January 13, 2009 4:38 AM

All replies

  • This may not be 100% correct, but it appears to work.

    // Console application to copy the contents of an existing Word document  
    // to a new document. Requires a reference to the Microsoft Office 11.0  
    // Object Library (or similar).  
     
    using System;  
    using Word = Microsoft.Office.Interop.Word;  
     
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // Initialize a bogus parameter for arguments we don't care about  
            object oMissing = System.Reflection.Missing.Value;  
     
            // Set the paths for the old and new documents (edit as needed)  
            object oldDocPath = (object)@"c:\test.doc";  
            object newDocPath = (object)@"c:\test2.doc";  
     
            // Get a Word application and show it
            Word.Application app = new Word.Application();
     
            // Open the existing document  
            Word.Document oldDoc = app.Documents.Open(ref oldDocPath,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
     
            // Select entire contents, and copy to clipboard  
            oldDoc.ActiveWindow.Selection.WholeStory();  
            oldDoc.ActiveWindow.Selection.Copy();  
     
            // Create new document and paste from clipboard  
            Word.Document newDoc = app.Documents.Add(ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing);  
            newDoc.ActiveWindow.Selection.Paste();  
     
            // Save new document  
            app.ActiveDocument.SaveAs(ref newDocPath,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
     
            // Close the documents and quit the application  
            oldDoc.Close(ref oMissing, ref oMissing, ref oMissing);  
            newDoc.Close(ref oMissing, ref oMissing, ref oMissing);  
            app.Quit(ref oMissing, ref oMissing, ref oMissing);  
        }  
    • Proposed as answer by Harry Zhu Thursday, January 15, 2009 1:25 AM
    • Marked as answer by Harry Zhu Monday, January 19, 2009 7:52 AM
    Monday, January 12, 2009 3:46 AM
  • Or more simply:

    // Console application to save a copy of an existing Word document.  
    // Requires a reference to the Microsoft Office 11.0 Object Library (or similar).     
     
    using System;  
    using Word = Microsoft.Office.Interop.Word;  
     
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // Initialize a bogus parameter for arguments we don't care about     
            object oMissing = System.Reflection.Missing.Value;  
     
            // Set the paths for the old and new documents (edit as needed)     
            object oldDocPath = (object)@"c:\test.doc";  
            object newDocPath = (object)@"c:\test2.doc";  
     
            // Get a Word application     
            Word.Application app = new Word.Application();  
     
            // Open the existing document     
            Word.Document oldDoc = app.Documents.Open(ref oldDocPath,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
     
            // SaveAs  
            app.ActiveDocument.SaveAs(ref newDocPath,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
     
            // Close the document and quit the application     
            oldDoc.Close(ref oMissing, ref oMissing, ref oMissing);  
            app.Quit(ref oMissing, ref oMissing, ref oMissing);  
        }  
    }  
     
    • Proposed as answer by Harry Zhu Thursday, January 15, 2009 1:25 AM
    • Marked as answer by Harry Zhu Monday, January 19, 2009 7:52 AM
    Monday, January 12, 2009 3:51 AM
  • Really appreciate mfisher! I got it fixed. 

    But now I got a new problem. 

    The thing is that the old word document has some changes track mark on the text and some comments were added. When I copy the whole old document to the new word document, i want to copy those changes mark and the comments as well. Is there any solution for that?

    Thanks advance!!!


    Monday, January 12, 2009 4:23 PM
  • pkminicooper,

    Have you thought about simply copying the entire file?  If you're not doing anything else to the file, or if the first thing you want to do is copy everything, just use File.Copy instead, and then alter the file using interop after that.  There may not be any need to go into Word Interop at all here.
    David Morton - http://blog.davemorton.net/
    Monday, January 12, 2009 4:28 PM
    Moderator
  • Yes, David's right. Unless you have some other task in mind which requires interop, File.Copy would be a much more direct solution. If the file has to be opened after copying, Process.Start would do it.

    With some basic exception handling to prevent the program from crashing, it might go like this:

    // Copy a file and open it using the default application.  
     
    using System;  
    using System.Diagnostics;  
    using System.IO;  
     
    class Program  
    {  
        static void Main()  
        {  
            string sourceFileName = @"c:\test.doc";  
            string destFileName = @"c:\testCopy.doc";  
            bool overwrite = true;  
     
            bool success = false;  
     
            if (File.Exists(sourceFileName))  
            {  
                try 
                {  
                    File.Copy(sourceFileName, destFileName, overwrite);  
                    success = true;  
                }  
                catch (IOException ex)  
                {  
                    Console.WriteLine("IO Exception: {0}", ex.Message);  
                }  
            }
            else
            {
                Console.WriteLine("File does not exist: {0}", sourceFileName);              
            } 
     
            if (success)  
            {  
                try 
                {  
                    Process.Start(destFileName);  
                }  
                catch (Exception ex)  
                {  
                    Console.WriteLine("Could not start process: {0}", ex.Message);  
                }  
            }  
        }  
    }  
     
    • Edited by Matt Fisher Tuesday, January 13, 2009 4:47 AM Corrected code errors
    • Proposed as answer by Ernst KuschkeModerator Tuesday, January 13, 2009 7:46 AM
    • Marked as answer by Harry Zhu Monday, January 19, 2009 7:52 AM
    Tuesday, January 13, 2009 4:38 AM