C# word 拼写 语法

来源:互联网 发布:电脑网络参数在哪里 编辑:程序博客网 时间:2024/05/01 23:27

1.引用word库

2.1

 try
            {
                object oMissing = System.Reflection.Missing.Value;
                //创建Word对象和临时文档
                Microsoft.Office.Interop.Word._Application wdApp = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word._Document wdDoc = wdApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
               
                //声明IDataObject存放从剪贴板返回的数据
                IDataObject iData;

                //定位Word窗口,使之不可见
                wdApp.WindowState = 0;
                wdApp.Top = -3000;

                //复制输入框的文字到剪贴板
                Clipboard.SetDataObject(txtWord.Text);

                //对临时文档进行拼写或者语法检查
                wdDoc.Content.Paste();
                wdDoc.Activate();

                object ignoreUppercase = true;
                object alwaysSuggest = true;
                object o = false;
                if (blnSpellOnly)
                {

                    wdDoc.CheckSpelling(ref oMissing, ref ignoreUppercase, ref alwaysSuggest, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
                        oMissing, ref oMissing);
                }
                else
                {
                    wdDoc.SpellingChecked = true;
                    wdDoc.CheckGrammar();
                }

                //在拼写检查或者语法检查后,如果用户进行了修改,

                //利用剪贴板把修改后的数据返回给文本输入框
                wdDoc.Content.Copy();
                iData = Clipboard.GetDataObject();
                if(iData.GetDataPresent(DataFormats.Text))
                {
                    txtWord.Text = (string)(iData.GetData(DataFormats.Text));
                }
                wdDoc.Saved = true;
                wdDoc.Close(ref o, ref oMissing, ref oMissing);

                wdApp.Quit(ref o, ref oMissing, ref oMissing);
                //MessageBox.Show("拼写检查已经完成!", "拼写检查", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Runtime.InteropServices.COMException COMExcep)
            {
                MessageBox.Show("必须安装Microsoft Word才能进行拼写或者语法检查。", "拼写检查", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception Excep)
            {
                MessageBox.Show("错误:" + Excep.Message, "拼写检查", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

 

2.2

            object oMissing = System.Reflection.Missing.Value;
            object timeOut = Type.Missing;
            object o = false;
            Microsoft.Office.Interop.Word._Application wdApp = new Microsoft.Office.Interop.Word.Application();

            wdApp.Visible = false;
            Microsoft.Office.Interop.Word._Document wdDoc = wdApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            wdApp.Selection.Text = txtWord.Text;
            int iCount = wdApp.Dialogs[WdWordDialog.wdDialogToolsSpellingAndGrammar].Show(ref timeOut); //A.无法判断他的忽略,B.Cancel 与 Replace 、Replace All 都返回-1 对话框是返回0
            if (iCount != 0)
            {
                MessageBox.Show("The spelling check is complete.", "Title", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            txtWord.Text = wdApp.Selection.Text;
            wdDoc.Saved = true;
            wdDoc.Close(ref o, ref oMissing, ref oMissing);
            wdDoc = null;
            //Marshal.ReleaseComObject(wd);
          
            wdApp.Quit(ref o, ref oMissing, ref oMissing);
            wdApp = null;

第一种情况不能同时检查拼写与语法。

同时这两种情况都不无做成与word一样的效果(指提示信息),A.无法判断他的忽略,B.Cancl 与 Replace 、Replace All 都返回-1 对话框是返回0

C.正确与以前就忽略的情况不需要弹出对话框???

原创粉丝点击