gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:38:16 24 4
gpt4 key购买 nike

我有一个带有表格的预制 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com