1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
/// <summary>
/// Rechtschreibprüfung mit Word.
/// </summary>
public class SpellChecker
{
/// <summary>
/// Startet Microsoft Word im Hintergrund und gibt ein Word.Application-Objekt davon zurück.
/// </summary>
public static object StartWinword()
{
// Typen-Information für Word abfragen
Type wordType = Type.GetTypeFromProgID("Word.Application");
// Wenn Word installiert ist ...
if (wordType != null)
{
// Word-Instanz erzeugen
object wordApp=Activator.CreateInstance(wordType);
// Dokumenten-Auflistung adressieren
object documents = wordType.InvokeMember("Documents", BindingFlags.GetProperty | BindingFlags.OptionalParamBinding, null, wordApp, new object[0]);
// Leeres Dokument einfügen
documents.GetType().InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, documents, new object[0]);
// Word-Instanz zurückgeben
return wordApp;
}
else
// Ausnahme werfen
throw new ApplicationException("Microsoft Word ist nicht installiert! Die Rechtschreibprüfung benötigt Microsoft Word.");
}
/// <summary>
/// Beendet eine bestimmte Instanz von microsoft Word.
/// </summary>
/// <param name="wordApp">Word.Application-Objekt</param>
public static void QuitWinword(object wordApp)
{
// Typen-Information des Word.Application-Objekt ermitteln
Type wordType = wordApp.GetType();
// Dokumenten-Auflistung adressieren
object documents = wordType.InvokeMember("Documents", BindingFlags.GetProperty | BindingFlags.OptionalParamBinding, null, wordApp, new object[0]);
// Leeres Dokument einfügen
documents.GetType().InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, documents, new object[0]);
// Word schließen
wordType.InvokeMember("Quit", BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, wordApp, new object[1] { 0 });
// Word-Instanz entsorgen
Marshal.FinalReleaseComObject(wordApp);
}
/// <summary>
/// Führt eine Rechtschreibprüfung für einen bestimmten Text durch.
/// </summary>
/// <param name="wordApp">Word.Application-Objekt</param>
/// <param name="text">Text</param>
/// <returns>Liste der Wörter, die falsch geschrieben sind</returns>
public static IList<string> CheckSpelling(object wordApp, string text)
{
// Typen-Information des Word.Application-Objekt ermitteln
Type wordType = wordApp.GetType();
// Liste für falsche Wörter erzeugen
IList<string> wrongWords=new List<string>();
// Sonderzeichen aus Text entfernen
text = text.Replace('.', ' ');
text = text.Replace(',', ' ');
text = text.Replace(':', ' ');
text = text.Replace(';', ' ');
text = text.Replace('"', ' ');
text = text.Replace('(', ' ');
text = text.Replace(')', ' ');
text = text.Replace('[', ' ');
text = text.Replace(']', ' ');
text = text.Replace('!', ' ');
text = text.Replace('?', ' ');
text = text.Replace('/', ' ');
text = text.Replace('\n', ' ');
// Text in Wörter zerlegen
string[] words = text.Split(' ');
// Alle Wörter durchlaufen
foreach (string word in words)
{
// Rechtschreibung des Wortes prüfen
bool spellCheckResult = (bool)wordType.InvokeMember("CheckSpelling", BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, wordApp, new object[1] { word });
// Wenn das Wort nicht richtig geschrieben ist ...
if (!spellCheckResult)
{
// Wort zur Liste zufügen
wrongWords.Add(word.Trim());
}
}
// Liste zurückgeben
return wrongWords;
}
}
|