1

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

目前,当单击任务窗格上的按钮时,我只是在运行时收到“命令失败”异常。这里是全班。如您所见,我尝试了两种方法,但都失败了:

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
        }
    }
}

任何帮助表示赞赏。

4

1 回答 1

0

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

作为一种解决方法,您可以Selection按如下方式使用该对象:

ActiveDocument.Tables(1).Range.Select();
Application.Selection.Collapse();
Application.Selection.InsertRowsAbove();
于 2015-12-02T16:10:16.240 回答