System.AccessViolationException 在 Comment.DeleteRecursively 上的 Word 中
System.AccessViolationException in Word on Comment.DeleteRecursively
我正在尝试从 Word 2010 文档中删除评论。我自己添加了它们,无论我在哪里发现不符合公司标准的字体。
但是每当我调用 DeleteRecursively 时,我都会得到 System.AccessViolationException。使用 VBA 删除评论运行没有任何错误。对象方法不同,因为在 VBA 我有 Comments(i).Delete 而不是 deleteRecursively.
我的产品版本是 14.0.7015.1000。
foreach (Word.Paragraph para in storyRange.Paragraphs)
{
if (Helper.ParagraphHasWrongFont(para))
{
Word.Range anchor = para.Range;
if (anchor.Comments.Count == 0)
{
Word.Comment comment = anchor.Comments.Add(anchor, "The font is wrong in this one.");
comment.Author = "System";
}
}
}
for (int i = ActiveDocument.Comments.Count - 1; i >= 1; i--)
{
if (ActiveDocument.Comments[i].Author == "System")
{
Word.Comment cmt = ActiveDocument.Comments[i];
cmt.DeleteRecursively();
}
}
错误消息具有误导性。我使用的是对 Office 2010 的引用,但方法 DeleteRecursively() 是 Office 2013 的新功能。
我用 comment.Delete() 替换了调用,它在旧版本的 Office 中可用。
我正在尝试从 Word 2010 文档中删除评论。我自己添加了它们,无论我在哪里发现不符合公司标准的字体。
但是每当我调用 DeleteRecursively 时,我都会得到 System.AccessViolationException。使用 VBA 删除评论运行没有任何错误。对象方法不同,因为在 VBA 我有 Comments(i).Delete 而不是 deleteRecursively.
我的产品版本是 14.0.7015.1000。
foreach (Word.Paragraph para in storyRange.Paragraphs)
{
if (Helper.ParagraphHasWrongFont(para))
{
Word.Range anchor = para.Range;
if (anchor.Comments.Count == 0)
{
Word.Comment comment = anchor.Comments.Add(anchor, "The font is wrong in this one.");
comment.Author = "System";
}
}
}
for (int i = ActiveDocument.Comments.Count - 1; i >= 1; i--)
{
if (ActiveDocument.Comments[i].Author == "System")
{
Word.Comment cmt = ActiveDocument.Comments[i];
cmt.DeleteRecursively();
}
}
错误消息具有误导性。我使用的是对 Office 2010 的引用,但方法 DeleteRecursively() 是 Office 2013 的新功能。
我用 comment.Delete() 替换了调用,它在旧版本的 Office 中可用。