下面的代码示例演示如何在 Visual C# 应用程序中通过 COM 互操作性使用 Word 的拼写检查功能。有关更多信息,请参见
示例
此示例演示如何从 C# 应用程序使用 Word 的拼写检查器。它使用 COM 互操作性创建一个新的 Word.application 对象。然后使用 Range 对象上的 ProofreadingErrors 集合并查找该范围中有拼写错误的单词。
| C# |  复制代码 | 
|---|---|
| using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
namespace WordSpell
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        public Form1()  //constructor
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, System.EventArgs e)
        {
            Word.Application app = new Word.Application();
            int errors = 0;
            if (textBox1.Text.Length > 0)
            {
                app.Visible = false;
                // Setting these variables is comparable to passing null to the function.
                // This is necessary because the C# null cannot be passed by reference.
                object template = Missing.Value;
                object newTemplate = Missing.Value;
                object documentType = Missing.Value;
                object visible = true;
                Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
                doc1.Words.First.InsertBefore(textBox1.Text);
                Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors;
                errors = spellErrorsColl.Count;
                object optional = Missing.Value;
                doc1.CheckSpelling(
                    ref optional, ref optional, ref optional, ref optional, ref optional, ref optional,
                    ref optional, ref optional, ref optional, ref optional, ref optional, ref optional);
                label1.Text = errors + " errors corrected ";
                object first = 0;
                object last = doc1.Characters.Count - 1;
                textBox1.Text = doc1.Range(ref first, ref last).Text;
            }
            object saveChanges = false;
            object originalFormat = Missing.Value;
            object routeDocument = Missing.Value;
            app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
        }
    }
}
 | |
编译代码
此示例要求系统上安装有 Word,并根据已安装的 Office 版本的不同,Word 程序集可能名为“Microsoft office 10 对象库”或“Word 11 对象库”。
|  注意 | 
|---|
| 显示的对话框和菜单命令可能会与“帮助”中描述的不同,具体取决于您现用的设置或版本。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见  | 
编译代码
- 
              
                在 Visual Studio 中创建一个新的 C# Windows 应用程序项目,并将它命名为 WordSpell。 
- 
              
                复制上面的代码并将其粘贴到 Form1.cs文件中以覆盖原来的内容。
- 
              
                复制下面的代码,并将它粘贴到 Form1.Designer.cs文件的InitializeComponent()方法中现有代码的后面。C#  复制代码 复制代码this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(40, 40); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Size = new System.Drawing.Size(344, 136); this.textBox1.TabIndex = 0; this.textBox1.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(392, 40); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(96, 23); this.button1.TabIndex = 1; this.button1.Text = "Check Spelling"; this.button1.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.Location = new System.Drawing.Point(40, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(336, 16); this.label1.TabIndex = 2; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(5, 13); this.ClientSize = new System.Drawing.Size(496, 205); this.Controls.Add(this.label1); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "SpellCheckDemo"; this.ResumeLayout(false); 
- 
              
                将 Word 程序集作为引用添加到该项目中。右击该项目,单击“添加引用”,再单击“添加引用”对话框的“COM”选项卡。双击“Microsoft Office 11 对象库”,然后按“确定”。 注意,显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置。 
安全
若要使用 COM 互操作性,您必须具有管理员或超级用户安全权限。有关更多信息,请参见
 
      
    
     
      
    
     
      
    
    