2015-12-01 58 views
1

我有一个VSTO项目,其中单击功能区上的按钮,打开具有特定模板的新Word文档并使用自定义任务窗格打开它。在该窗格上有一个按钮,点击该按钮时,我想将一行添加到存在于文档模板中的表格中。VSTO Word - 在任务窗格中单击按钮时添加表格行

目前我只是在运行时点击任务窗格上的按钮时出现'command failed'异常。这是全班同学。正如你可以看到我已经尝试了两种方式来做到这一点,无论是失败的:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using word = Microsoft.Office.Interop.Word; 

namespace TestWordAddin2010 
{ 
    public partial class NewDescDMtaskPane : UserControl 
    { 
     public NewDescDMtaskPane() 
     { 
      InitializeComponent(); 
     } 

     private void addSpare_Click(object sender, EventArgs e) 
     { 
      Globals.ThisAddIn.Application.ActiveDocument.Tables[1].Rows.Add(Globals.ThisAddIn.Application.ActiveDocument.Tables[1].Rows[1]); //this doesn't work 

      //word.Table wordTable = Globals.ThisAddIn.Application.ActiveDocument.Tables[1]; 
      //wordTable.Rows.Add(wordTable.Rows[1]); //neither does this work 
     } 
    } 
} 

任何帮助表示赞赏。

+1

以何种方式它不工作:

作为一种变通方法,你可以按如下方式使用Selection对象?你有错误吗?该行是否添加了错误的地方?请提供更多信息。 –

+0

我在Visual Studio中出现'Command Failed'异常。没有行被添加到任何表中。 –

+0

啊,我的错。我在Content Controls的顶部有一个隐藏表格。当我将它更改为'Tables [2]'时,它向我想要的表格添加了一行。但是,我实际上也打算在该表中使用内容控件。这是否意味着我不能将行添加到具有内容控件的表中?显然我可以尝试,但你可能知道答案? –

回答

0

很有可能您的第一个表格包含合并的单元格。然后,您将无法访问Rows集合中的单个行。

ActiveDocument.Tables(1).Range.Select(); 
Application.Selection.Collapse(); 
Application.Selection.InsertRowsAbove(); 
相关问题