2015-06-17 91 views
0

我想使用Microsoft.Interop.Word以编程方式打开Word文档,并将注释插入表格单元格。 我有单元格范围的开始和结束位置。 (Range.Start \ Range.End)Microsoft Interops 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崩溃

+0

Word崩溃?没有错误信息? Word是否正确安装? Word的最佳版本?请添加您的问题的答案! –

+0

Microsoft Word 2013,Word安装正确且没有错误信息,没有例外,只是崩溃“Microsoft Word已停止工作”,然后重新启动Microsoft Word。如果我在平面文本中插入评论,它效果很好 –

+0

Interops版本14.0.0.0 –

回答

0

我你的代码转换为有了一些变化。 它工作正常,没有错误! 看到它

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;