Microsoft Interops Word将注释插入表格单元崩溃单词

时间:2015-06-17 07:53:29

标签: c# .net ms-word interopservices

我希望使用Microsoft.Interop.Word以编程方式打开word文档,并在表格单元格中插入注释。 我有单元格范围的开始和结束位置。 (Range.Start \ Range.End)

Application.ActiveDocument.Select(); // select ative document
Range rg = Application.Selection.Range; // get the range of the current selection (all document)


 if (rg.Tables.Count > 0)
 {

     Microsoft.Office.Interop.Word.Range rngTab = rg;

     //set the coordinate of the Range of the text 
     rngTab.Start = startRng;
     rngTab.End = endRng;


     doc.ActiveWindow.Visible = true;
     rg.Select();
     Application.ActiveDocument.Comments.Add(rngTab, ref commentText);                                           
  }

插入评论时Word崩溃

1 个答案:

答案 0 :(得分:0)

我将您的代码转换为 pascal 并进行了一些更改。 工作正常,没有错误!  看到它

var WApplication:twordapplication; rg,rngTab:range;
commentText:olevariant;
begin
commentText:='123';
WApplication := twordapplication.Create(form1);
WApplication.Connect;
WApplication.Visible:=true;
WApplication.Activate;
rg:= WApplication.Selection.Range; // get the range of the current selection (all document)
rngTab:= rg;
     //set the coordinate of the Range of the text
     rngTab.Start:= 1;
     rngTab.End_:= 2;
     rg.Select();
     WApplication.ActiveDocument.Comments.Add(rngTab, commentText);// add comment to text
     rngtab:=WApplication.ActiveDocument.Tables.Item(1).cell(2,2).range;
     rngtab.Select();
     WApplication.ActiveDocument.Comments.Add(rngTab, commentText); // add comment to cell in the table
end;