开发者

Is there an easy way to copy a word doc. into another using C#?

开发者 https://www.devze.com 2023-02-28 11:48 出处:网络
How can I copy contents of one word document and insert it in anothe开发者_Go百科r pre-existing word document using C#. I\'ve had a look around but everything looks really complicated (I\'m a newbie).

How can I copy contents of one word document and insert it in anothe开发者_Go百科r pre-existing word document using C#. I've had a look around but everything looks really complicated (I'm a newbie). Surely there must be an easy solution?

I have found this code which gives me no errors but it doesnt seem to be doing anything. It's certainly not copying into the correct word doc. put it that way.

Word.Application oWord = new Word.Application();

Word.Document oWordDoc = new Word.Document();
Object oMissing = System.Reflection.Missing.Value;
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

oWordDoc.ActiveWindow.Selection.WholeStory();
oWordDoc.ActiveWindow.Selection.Copy();

oWord.ActiveDocument.Select();
oWordDoc.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

P.S these word docs are .doc


        Word.Application oWord = new Word.Application();

        Word.Document oWordDoc = new Word.Document();
        Object oMissing = System.Reflection.Missing.Value;
        object oTemplatePath = @"C:\\Documents and Settings\\Student\\Desktop\\ExportFiles\\" + "The_One.docx"; 
        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
        oWordDoc.ActiveWindow.Selection.WholeStory();
        oWordDoc.ActiveWindow.Selection.Copy();
        oWord.ActiveDocument.Select();
        // The Visible flag is what you've missed. You actually succeeded in making
        // the copy, but because
        // Your Word app remained hidden and the newly created document unsaved, you could not 
        // See the results.
        oWord.Visible = true;
        oWordDoc.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);


It's funny to see all the C# guys asking now the questions the VBA developers have answered since 15 years. It's worth to dig in VB 6 and VBA code samples, if you have to work with Microsoft Office automation issues.

For the point "nothing happens" it's simple: if you start Word through automation, you must set the application also to visible. If you run your code, it will work, but Word remains an invisible instance (open Windows Task Manager to see it).

For the point "easy solution", you can try to insert a document at a given range with the InsertFile method of the range, e.g. like this:

        static void Main(string[] args)
    {

        Word.Application oWord = new Word.Application();
        oWord.Visible = true;  // shows Word application

        Word.Document oWordDoc = new Word.Document();
        Object oMissing = System.Reflection.Missing.Value;
        oWordDoc = oWord.Documents.Add(ref oMissing);
        Word.Range r = oWordDoc.Range();
        r.InsertAfter("Some text added through automation!");
        r.InsertParagraphAfter();
        r.InsertParagraphAfter();
        r.Collapse(Word.WdCollapseDirection.wdCollapseEnd);  // Moves range at the end of the text
        string path = @"C:\Temp\Letter.doc";
         // Insert whole Word document at the given range, omitting page layout
         // of the inserted document (if it doesn't contain section breakts)
        r.InsertFile(path, ref  oMissing, ref oMissing, ref oMissing, ref oMissing);

    }

NOTE: I was using framework 4.0 for this example, which allows for optional parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号