2017-04-06 65 views
0

我想從Word 2010文檔中刪除註釋。無論我在哪裏找到不符合公司標準的字體,我都自己添加了它們。System.AccessViolationException在Word中Comment.Delete遞歸

但是每當我調用DeleteRecursively,我得到一個System.AccessViolationException。使用VBA刪除註釋運行沒有任何錯誤。對象方法不同,因爲在VBA中我有評論(i)。刪除而不是刪除。

我有一個產品版本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(); 
      } 
     } 

回答

0

錯誤消息是誤導性的。我使用的是對Office 2010的引用,但DeleteRecursively()方法是Office 2013的一個較新功能。

我用comment.Delete()替換了調用,它在舊版Office中可用。

MSDN forum link