none
Copy paragraphs from one word file and paste it to another word file along with formatting RRS feed

  • Question

  • Hi,

    How do we copy paragraphs from one word file and paste it to another word file along with formatting using c#?

    Regards,

    Vaibhav

    • Moved by Alexander Sun Friday, May 11, 2012 8:11 AM Move to more appropriate forum (From:Visual C# Language)
    Thursday, May 10, 2012 8:59 AM

Answers

  • Hi Vaibhav,

    Welcome to the MSDN forum!

    Please refer to the following code:

                Word.Application oWord = new Word.Application();
                oWord.Visible = true;
                Word.Document oDoc1 = oWord.Documents.Add();
                Word.Document oDoc2 = oWord.Documents.Open(@"E:\00-Word\Test.docx");
    
                Word.Range oRange = oDoc2.Content;
                oRange.Copy();            
                oDoc1.Content.PasteSpecial(DataType: Word.WdPasteOptions.wdKeepSourceFormatting);

    Thanks.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 14, 2012 7:38 AM
    Moderator

All replies

  • Thursday, May 10, 2012 9:15 AM
  • Hi Vaibhav,

    Welcome to the MSDN forum!

    Please refer to the following code:

                Word.Application oWord = new Word.Application();
                oWord.Visible = true;
                Word.Document oDoc1 = oWord.Documents.Add();
                Word.Document oDoc2 = oWord.Documents.Open(@"E:\00-Word\Test.docx");
    
                Word.Range oRange = oDoc2.Content;
                oRange.Copy();            
                oDoc1.Content.PasteSpecial(DataType: Word.WdPasteOptions.wdKeepSourceFormatting);

    Thanks.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 14, 2012 7:38 AM
    Moderator
  • Hi Yoyo Jiang,

    Your Answer is work for me but not completely.

    I am trying to add two page word document into active document object with active document formatting.

    I can able to paste only one page not two pages form doc2. even its rang object has start and end position.

    Please update me for this issue.

    See below my code:

                   object start = 0;
                    start = winword.ActiveDocument.Content.End - 1;
                    // create another range object with the new value for start
                    Range rng1 = document.Range(ref start, ref missing);

    Microsoft.Office.Interop.Word.Document doc2 = winword.Documents.Open(@"f:\Neograftquote1.docx");
                    object nstart = 0;
                    object nend = 0;
                    nstart = doc2.Content.Start;
                    nend = doc2.Content.End - 1;
                    Range orange = doc2.Range(ref nstart, ref nend);
                    //Range orange = doc2.Content;
                    orange.Copy();
                    rng1.PasteSpecial(DataType: WdPasteOptions.wdKeepSourceFormatting);


                    // now make start to point to the end of the content of the first document
                    start = winword.ActiveDocument.Content.End - 1;
                    //make the word appliction visible
                    winword.Visible = true;

    Friday, November 20, 2015 3:55 PM
  • Hi Yoyo,

    I fixed the issue and also set format what i want.

    Thanks for help.

    Friday, November 20, 2015 4:30 PM
  • Hi Yoyo,

    I fixed the issue and also set format what i want.

    Thanks for help.

    Hi softHet

    Where is the solution .i have the little difference way. What i want that is i have standard document and onther document called the template and it's Dynamic i already have replace the keys successfully using interop.word library 

    but what am trying now to do how i marge the template document and the original standard document????

    i

      public ActionResult DownloadDocument(int id)
            {
                //get Standard & User
                var doc = standardMgmt.GetStandardsDoc(id);
                User user = getCurrentUser();
                Standard currentStandard = doc.Standard;
                string now = DateTime.Now.ToString();
              
    
    
    
                Microsoft.Office.Interop.Word.Application appWord2 = new Microsoft.Office.Interop.Word.Application { Visible = false };
    
    
    
                //Get Cover Path & //Add Footer
                string footer = null;
                string coverPath = null;
                if (user.UserRole.IsSFDA){
                    coverPath = Server.MapPath(@"~\CoverDoc\InternalCover.docx");
                    footer = " باستخدام هذه المواصفة بشكل فردي فقط ولا يجوز نسخها أو منح الأذن لأي طرف أخر باستخدامها (" + user.UserName + ")   جميع الحقوق محفوظة للهيئة العامة للغذاء والدواء ويسمح للـ (" + now + ")";
                   
                }
                else
                {
                    coverPath = Server.MapPath(@"~\CoverDoc\InternalCover.docx");
                    //footer = " جميع الحقوق محفوظة للهيئة العامة للغذاء والدواء ويسمح للـ(" + user.UserName + ") باستخدام هذه المواصفة داخلياً لأغراض العمل فقط ولا يجوز نسخها أو منح الأذن لأي طرف أخر باستخدامها   (" + now + ")";
                    footer = " باستخدام هذه المواصفة بشكل فردي فقط ولا يجوز نسخها أو منح الأذن لأي طرف أخر باستخدامها (" + user.UserName + ")   جميع الحقوق محفوظة للهيئة العامة للغذاء والدواء ويسمح للـ (" + now + ")";
                }
                    
    
                //copy the cover to the temp filder
                System.IO.File.Copy(coverPath, Server.MapPath(@"~\App_Data\Temp\test.docx"),true);
                string tempTrmplateCover = Server.MapPath(@"~\App_Data\Temp\test.docx");
                //Get Cover Document
                Microsoft.Office.Interop.Word.Document CoverDocument = appWord2.Documents.Open(tempTrmplateCover, ReadOnly: false, Visible: true);
    
                CoverDocument.Activate();
    
                //CoverDocument.Application.Selection.Range.InsertBefore("CoverDocument.docx");
                // [find and replace]
                FindAndReplace(appWord2, "@StnadradNumber", currentStandard.StandardNumber);
                FindAndReplace(appWord2, "@StandardName", currentStandard.TitleArabic);
                FindAndReplace(appWord2, "@ICS", currentStandard.ICS);
                FindAndReplace(appWord2, "أسم الموظف", user.FirstName + user.LastName);
                FindAndReplace(appWord2, "التاريخ", DateTime.Now);
                //FindAndReplace(appWord2, "test1", "hussam");
                //FindAndReplace(appWord2, "test2", "hussam");
    
    
                foreach (Microsoft.Office.Interop.Word.Section wordSection in CoverDocument.Sections)
                {
                    Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    footerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
                    footerRange.Font.Size = 20;
                    footerRange.Text = footer;
                }
    
    
                Byte[] filebyts = doc.Data;
                string docsTempPath = Server.MapPath(@"~\App_Data\Temp");
                System.IO.File.WriteAllBytes(Path.Combine(docsTempPath, doc.FileName), filebyts);
    
    
    
    //Merage
    ??????
    
                CoverDocument.ExportAsFixedFormat(@"D:\Hussam.pdf", WdExportFormat.wdExportFormatPDF);


    • Edited by Develpoer Tuesday, December 29, 2015 7:19 AM
    Tuesday, December 29, 2015 7:16 AM