Microsoft互操作词将注释插入表单元格崩溃词

本文关键字:表单 单元格 插入 崩溃 注释 互操作 Microsoft | 更新日期: 2023-09-27 17:56:36

我想使用 Microsoft.Interop.Word 以编程方式打开一个 Word 文档,并在表格单元格中插入注释。我有单元格范围的开始和结束位置。(范围.开始 '' 范围.结束)

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 崩溃

Microsoft互操作词将注释插入表单元格崩溃词

我通过一些更改将您的代码转换为 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;