使用VSTO禁用Word文档中的拼写检查

时间:2013-11-08 12:27:12

标签: c# ms-word vsto

我正在尝试使用VSTO加载项禁用当前活动的Word文档中的拼写检查。我想避免在文档的Open XML标记中保存拼写错误。

我尝试使用Range.NoProofing属性。

currentDocument.Content.NoProofing = 1;

MSDN documentation表示应将其设置为true,但该属性的类型为int。我尝试将其设置为1,但它不起作用(拼写检查错误仍然出现在文档中)。在调试器中,我看到在赋值后该属性仍设置为0

如何正确使用Range.NoProofing属性,还是有其他方法可以禁用Word文档中的拼写检查?

1 个答案:

答案 0 :(得分:1)

在使用DocumentBeforeSave事件保存文档之前,我最终忽略了所有拼写错误。

this.application.DocumentBeforeSave += this.OnDocumentBeforeSave;

NoProofing属性在包含拼写错误的范围内使用时似乎可以正常工作。

private void OnDocumentBeforeSave(Document doc, ref bool saveAsUi, ref bool cancel)
{
    // Ignore all spelling errors in the document
    foreach (Range error in this.application.ActiveDocument.SpellingErrors)
    {
        error.NoProofing = 1;
    }

    // Ignore all spelling errors in content controls
    foreach (ContentControl control in this.application.ActiveDocument.ContentControls)
    {
        control.Range.NoProofing = 1;
    }
}