2011-05-30 81 views
2

我有一個預先製作的具有表格的Word模板。我想打開它,然後在文檔的末尾添加(粘貼)另一個表格。問題是它不會到達文檔的末尾,而是將新表粘貼到原始表的第一個單元格中。任何幫助將不勝感激。如何將表格粘貼到使用C#的Ms-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); 
+0

你既嘗試開始和結束= oDoc.Content.End? – Fosco 2011-05-30 01:51:39

+1

嘗試過,但獲得超出範圍的錯誤。 – user770344 2011-05-30 02:02:41

回答

1

我已經找到了答案!

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

  Object objUnit = Word.WdUnits.wdStory; 

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

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

更改以下行代碼

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

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

希望這有助於...

+0

我更新了我粘貼到論壇的內容。它應該讀取 對象開始= oWord.Selection.End; object end = oWord.Selection.End; – user770344 2011-05-30 05:25:33

+0

其他:如果我使用oDoc.Content.Start;最後,它會覆蓋模板中已有的內容。 – user770344 2011-05-30 05:38:42

+0

不,它不會覆蓋任何內容,只會附加表格,並且對我來說工作正常... – 2011-05-30 05:42:38

0
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; 
} 
相關問題