Microsoft Interops Word Insert Comment into Table Cell Crash Word

691 views Asked by At

i want open a word document programmatically using Microsoft.Interop.Word and insert a comment into a table cell. I have the start and End position of Range of the cell. (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);                                           
  }

When insert comment Word Crashes

1

There are 1 answers

0
Ivan.s On

I convert your code to pascal with some changes. It work fine, without error! See it

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;