以程式設計方式在文件中檢查拼字

若要在文件中檢查拼字,請使用 CheckSpelling 方法。 這個方法會傳回布林值,指出所提供的參數拼字是否正確。

適用對象:本主題資訊適用於文件層級的專案和 Word 的 VSTO 增益集專案。 如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

若要在訊息方塊中檢查拼字並顯示結果

  1. 呼叫 CheckSpelling 方法並向其傳遞文字範圍以檢查是否有拼字錯誤。 若要使用這個程式碼範例,請從專案的 ThisDocumentThisAddIn 類別中執行它。

    string result = "Spelled incorrectly.";
    
    object startLocation = this.Content.Start;
    object endLocation = this.Content.End;
    bool spellCheck = this.Application.CheckSpelling(
        this.Range(ref startLocation, ref endLocation).Text);
    
    if (spellCheck == true)
    {
        result = "Spelled correctly.";
    }
    
    MessageBox.Show(result);