如何使用C#表格粘贴到MS-Word文档的末尾 [英] How to paste table to the end of a Ms-Word document using C#

查看:643
本文介绍了如何使用C#表格粘贴到MS-Word文档的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一个表预制的Word模板。我想打开它,然后在文件的最后添加(粘贴)另一个表。的问题是,它不会转到这个文件的结尾,而不是它的新表粘贴到原始表的第一个单元。任何帮助将不胜感激。

  //上面的代码从另一个文件

复制对象的表oTempPath =C:\\Temp\\Logtemp.doc
对象defaultTemplate =C:\\Temp\\LogContemp.doc


沃达柯= oWord.Documents.Open(REF defaultTemplate,
裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,
裁判oMissing,裁判oMissing,裁判oMissing,楼盘oMissing,
裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,
裁判oMissing,裁判oMissing,裁判oMissing);



对象开始= oDoc.Content.Start;
对象最终= oDoc.Content.End;
oDoc.Range(REF开始,裁判结束).Copy();


oDoc.Close(REF oMissed,裁判oMissed,裁判oMissed);


沃达柯= oWord.Documents.Open(REF oTempPath,
裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,
裁判oMissing,裁判oMissing,裁判oMissing,楼盘oMissing,
裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,
裁判oMissing,裁判oMissing,裁判oMissing);

oDoc.Activate();

// ****这是问题开始的地方****

开始= oWord.Selection.End;
端= oWord.Selection.End;



Word.Range RNG = oDoc.Range(REF开始,裁判结束);


rng.Select();
rng.Paste();


对象fileN1 =C:\\temp\\+ TextBox1.Text +Log.doc

oDoc.Fields.Update();
oDoc.SaveAs(REF fileN1,
裁判oMissed,裁判oMissed,裁判oMissed,裁判oMissed,裁判oMissed,
裁判oMissed,裁判oMissed,裁判oMissed,裁判oMissed,裁判oMissed,
裁判oMissed,裁判oMissed,裁判oMissed,裁判oMissed,裁判oMissed);
oDoc.Close(REF oMissed,裁判oMissed,裁判oMissed);
oWord.Quit(REF oMissing,裁判oMissing,裁判oMissing);


解决方案

我已经找到了答案!



而不是使用Word.Range.Paste我用下面的:

 对象objUnit = Word.WdUnits.wdStory; 

oWord.Selection.EndKey(REF objUnit,裁判oMissing);

oWord.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);


I have a pre-made Word Template that has a table. I would like to open it up and then add (paste) another table at the end of the document. The problem is that it will not goto the end of the document, instead it paste the new table into the first cell of the original table. Any help would be greatly appreciated.

//previous code copied a table from another document

Object oTempPath = "C:\\Temp\\Logtemp.doc";
Object defaultTemplate = "C:\\Temp\\LogContemp.doc";


oDoc = oWord.Documents.Open(ref defaultTemplate,
                            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);



                        object start = oDoc.Content.Start;
                        object end = oDoc.Content.End; 
                        oDoc.Range(ref start, ref end).Copy();


                        oDoc.Close(ref oMissed, ref oMissed, ref oMissed);


                        oDoc = oWord.Documents.Open(ref oTempPath,
            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);

                        oDoc.Activate();

//**** This is where the issue starts ****

                        start = oWord.Selection.End;
                        end = oWord.Selection.End;



                       Word.Range rng = oDoc.Range(ref start, ref end);


                        rng.Select();
                        rng.Paste();


                        object fileN1 = "C:\\temp\\" + TextBox1.Text + " Log.doc";

                        oDoc.Fields.Update();
                        oDoc.SaveAs(ref fileN1,
                            ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed,
                            ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed,
                            ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed);
                        oDoc.Close(ref oMissed, ref oMissed, ref oMissed);
                        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

解决方案

I have found the answer!!

Instead of using Word.Range.Paste I used the following:

          Object objUnit = Word.WdUnits.wdStory;

          oWord.Selection.EndKey(ref objUnit, ref oMissing);

          oWord.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

这篇关于如何使用C#表格粘贴到MS-Word文档的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆