2012-10-31 42 views
0

我有一個充滿書籤的單詞模板,但是當我嘗試插入一個表時,我陷入了困境......首先,我發出了一個COMException,表示請求的集合成員不存在。意味着設置的書籤與我現在調用的表名稱不同。無論如何,表格根本不顯示..我已經在數據輸入到表格之前設置了格式...
像這樣:Word 2010表

// Insert Table 


      Word.Table tbl1 = this.Tables[1]; 
      Tables.Add(Range: tbl1.Range, NumColumns: 2, NumRows: 2); 
      tbl1.Range.Font.Size = 10; 
      tbl1.Range.Font.Name = "Georgia"; 
      tbl1.Range.Font.Bold.Equals(true); 
      tbl1.Range.Font.ColorIndex = Word.WdColorIndex.wdBlue; 
      tbl1.Range.Cells.Shading.Texture = Word.WdTextureIndex.wdTexture10Percent; 
      tbl1.Range.Cells.Shading.BackgroundPatternColorIndex = Word.WdColorIndex.wdBlue; 
      tbl1.Range.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; 
      tbl1.Rows.SetHeight(RowHeight: 24, HeightRule: Word.WdRowHeightRule.wdRowHeightAtLeast); 
      tbl1.Columns[1].SetWidth(ColumnWidth: 170, RulerStyle: Word.WdRulerStyle.wdAdjustNone); 
      tbl1.Columns[2].SetWidth(ColumnWidth: 310, RulerStyle: Word.WdRulerStyle.wdAdjustNone); 
      tbl1.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleNone; 
      tbl1.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleNone; 

      // end of table insert 

然後填充表是後的代碼...

if (multipleLimits.Equals(false)) 
      { 
       tbl1.Cell(1, 1).Range.Text = "Indemnity Limit:"; 
      } 
      else 
      { 
       tbl1.Cell(1, 1).Range.Text = IndemlimitsText(iIndemnLimit).ToString(); 
      } 


      switch (typeOfInsID) 
      { 
       case "4": 
        tbl1.Cell(1, 1).Range.Text = "Public/Products Liability:"; 
        break; 
      } 

      tbl1.Cell(2, 1).Range.Text = "Excess:"; 

      if (multipleLimits.Equals(false)) 
      { 
       tbl1.Cell(1, 2).Range.Text = sCurType + iIndemnLimit; 
      } 
      else 
      { 
       tbl1.Cell(1, 2).Range.Text = stripIndemLimitCode(iIndemnLimit).ToString(); 
      } 

等等...
我的問題確實是,即使沒有數據需要解析也應該顯示錶...如果我想按行測試解析數據行嗎?或者填充表格的代碼需要存在並且正確地爲表格顯示?
我希望我havnt狼吞虎嚥太多,困惑我的問題..
謝謝你們!

回答

0

答案是肯定的,表格應該顯示,即使所有單元格都是空的。 我猜你的範圍是這裏的問題,爲

object endOfDoc = "\\endofdoc"; 
object missing = System.Reflection.Missing.Value; 
string totRows = 2; 
string totColumns = 2; 

word.Range wrdRng = doc.Bookmarks.get_Item(ref endOfDoc).Range; 
oTable = doc.Tables.Add(wrdRng, totRows, totColumns, ref missing, ref missing); 

將2列2行添加表到文檔的末尾。你嘗試添加到表格的範圍(tbl1.Range),但是它可以彈出表格內部的範圍(包含你的單元格),你想添加到表格外的段落或其他東西。 endOfDoc是一個書籤,可用於將文檔添加到文檔的末尾。

因爲我不知道你使用的是什麼樣的模板,所以你可以先試一下,然後找出你需要的正確的書籤名稱。這可以用與上面的代碼中的endOfDoc相同的方式來定義,