根据微软词典生成单词列表 - 停止工作 - 为什么?

时间:2010-10-27 00:14:45

标签: .net spell-checking office-2007 office-interop hebrew

我写了一个小应用程序,生成单词然后根据Microsoft Word 2007字典验证它们。

当我在3字母长度的英语词典上测试时,这对我很有用(我想有更多的字母),但由于某种原因,当我尝试对希伯来字典运行它时,它会停止工作。

有人知道为什么吗?如果是,我该如何解决? 如果有更好的方法,如果有人可以指出,我会很高兴。

感谢。

更新:当我说它停止时,我的意思是它停止检查单词。我得到所有单词组合,直到结束,但它们没有被检查。

MainWindow.xaml

<Window x:Class="AllHebrewWords.CreateDictionary"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="List of words" Height="250" Width="150" Closing="Window_Closing" ResizeMode="CanMinimize">
    <Grid>
        <ListBox Margin="10,10,10,10" Name="WordsList" />
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Diagnostics;
using System.Reflection;
using System.Threading;
using Microsoft.Office.Interop.Word;

namespace AllHebrewWords
{
    public partial class CreateDictionary : System.Windows.Window
    {
        private Application app = null;
        private _Document doc = null;
        private Thread thread = null;
        object oMissing = Missing.Value;

        public CreateDictionary()
        {
            InitializeComponent();

            app = new Application();
            object visible = false;
            doc = app.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref visible);

            thread = new Thread(SearchThread);
            thread.Start();
        } // End of function

        public void SearchThread()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (char ch = 'א'; ch <= 'ת'; ch++)
            {
                AddLetter(ch.ToString());
            } // End of for

            object FileName = "C:/Words";
            object FileFormat = WdSaveFormat.wdFormatText;
            doc.SaveAs( ref FileName, ref FileFormat, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            stopwatch.Stop();

            Dispatcher.Invoke((ThreadStart)delegate() { WordsList.Items.Add("Dictionary ready"); });
            Dispatcher.Invoke((ThreadStart)delegate() { WordsList.Items.Add(stopwatch.Elapsed); });
            Dispatcher.Invoke((ThreadStart)delegate() { WordsList.ScrollIntoView(WordsList.Items[WordsList.Items.Count - 1]); });
        } // End of function

        public bool CheckWord(string word)
        {
           if (app.CheckSpelling(word))
        {
            doc.Words.Last.InsertAfter(word + '\n');
            return true;
        }
        return false;
        } // End of function

        public void AddLetter(string word)
        {
            CheckWord(word);

            if (word.Length < 3)
            {
                char ch = word[word.Length - 1];

                for (ch = 'א'; ch <= 'ת'; ch++)
                {
                    word += ch;
                    AddLetter(word);
                    word = word.Remove(word.Length - 1);
                } // End of for
            } // End of if
        } // End of function

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            object saveChanges = false;
            doc.Close(ref saveChanges, ref oMissing, ref oMissing);
            thread.Abort();
            app.Quit(ref saveChanges, ref oMissing, ref oMissing);
        } // End of function
    } // End of class
} // End of namespace

0 个答案:

没有答案