我正在尝试在Visual Studio 2008中为Microsoft Word 2007创建一个C#添加。添加应该在富文本内容控件中创建一个表,以便稍后添加可以重新引用该表表格以便编辑它。我已成功地在内容控件内创建表,并重新引用内容控件以更改表内容。不幸的是,在这样做之后,由于某种原因删除了内容控件,因此我将无法再次引用该表。
以下是创建表和内容控件的代码:
private String name = "Hello";
private void btnTest1_Click(object sender, RibbonControlEventArgs e)
{
Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;
if (Globals.ThisAddIn.Application == null)
return;
Document vstoDoc = Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject();
object start = Globals.ThisAddIn.Application.Selection.Start;
object end = Globals.ThisAddIn.Application.Selection.End;
object Unknown = Type.Missing;
Word.Range thisrange = Globals.ThisAddIn.Application.ActiveDocument.Range(ref start, ref end);
Word.Table oTable = vstoDoc.Tables.Add(thisrange, 2, 2,ref Unknown, ref Unknown);
oTable.Select();
richTextControl1 = vstoDoc.Controls.AddRichTextContentControl(name);
richTextControl1.Title = "Control " + name;
name += " again";
}
以下是引用内容控件并最终删除它的代码:
private void btnTest2_Click(object sender, RibbonControlEventArgs e)
{
Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;
Word.Table oTable;
if (Globals.ThisAddIn.Application == null)
return;
Document vstoDoc = Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject();
object Unknown = Type.Missing;
Word.ContentControls controls = vstoDoc.SelectContentControlsByTitle("Control Hello");
foreach (Word.ContentControl control in controls)
{
oTable = control.Range.Tables[1];
oTable.Cell(1,1).Range.Text = "Testing";
}
有没有人能解决这个问题?
答案 0 :(得分:0)
我认为您需要将控件的LockContentControl属性设置为true,或者将其设置为C#中所需的任何内容。