【问题标题】:How to paste table to the end of a Ms-Word document using C#如何使用 C# 将表格粘贴到 Ms-Word 文档的末尾
【发布时间】:2011-09-04 12:34:45
【问题描述】:

我有一个带有表格的预制 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 和 end = oDoc.Content.End 吗?
  • 试过了,但得到一个值超出范围的错误。

标签: c# ms-word document copy-paste


【解决方案1】:

更改以下代码行

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

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

希望这会有所帮助...

【讨论】:

  • 我更新了我粘贴到论坛的内容。它应该已经读取 object start = oWord.Selection.End;对象结束 = oWord.Selection.End;
  • 附加:如果我使用 oDoc.Content.Start;最后,它将覆盖模板中已有的内容。
  • 不,它不会覆盖任何内容,它只会附加表格,它对我来说很好......
  • 我也找到了另一个解决方案。感谢您的帮助!
【解决方案2】:

我找到答案了!!

我没有使用 Word.Range.Paste,而是使用了以下内容:

          Object objUnit = Word.WdUnits.wdStory;

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

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

【讨论】:

    【解决方案3】:
    Globals.ThisAddIn.Application.ActiveDocument.Range(
    Globals.ThisAddIn.Application.ActiveDocument.Content.End-1,
    Globals.ThisAddIn.Application.ActiveDocument.Content.End-1).Select();
    object missing = System.Type.Missing;
    Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
    Word.Table newTable = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(
    currentRange, 3, 4, ref missing, ref missing);
    
    // Get all of the borders except for the diagonal borders.
    Word.Border[] borders = new Word.Border[6];
    borders[0] = newTable.Borders[Word.WdBorderType.wdBorderLeft];
    borders[1] = newTable.Borders[Word.WdBorderType.wdBorderRight];
    borders[2] = newTable.Borders[Word.WdBorderType.wdBorderTop];
    borders[3] = newTable.Borders[Word.WdBorderType.wdBorderBottom];
    borders[4] = newTable.Borders[Word.WdBorderType.wdBorderHorizontal];
    borders[5] = newTable.Borders[Word.WdBorderType.wdBorderVertical];
    
    // Format each of the borders. 
    foreach (Word.Border border in borders)
    {
        border.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
        border.Color = Word.WdColor.wdColorBlue;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 2014-11-21
      • 2014-12-29
      • 1970-01-01
      相关资源
      最近更新 更多