c# - 如何使用 C# 将表格粘贴到 Ms-Word 文档的末尾

标签 c# ms-word document copy-paste

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

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

最佳答案

更改以下代码行

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

start = oDoc.Content.End - 1;
end = oDoc.Content.End;

希望这有帮助...

关于c# - 如何使用 C# 将表格粘贴到 Ms-Word 文档的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171623/

相关文章:

c# - 绑定(bind)到 WPF 中的另一个绑定(bind)

c# - 如何将两个 Entity Framework 调用合并为一个?

c# - 无法并行启动标记为 MainLauncher 的 Activity

c# - 有没有办法注入(inject)/交换扩展?

excel - 选择 Word 文档的内容并使用 VBA 将其粘贴到 Outlook 的正文中

c# - 如何将 tinymce 生成的 html 转换为 MS Word 文档?

java - 在 Liferay 文档库中授予权限

c# - 出错后重新启用 Microsoft Word 加载项

javascript - "document.forms is not a function"与 "document.forms(0)"

jQuery - $(document).ready 函数中的函数