Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 54
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Rechtschreibprüfung mit Word


Autor: Rainbird
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 9662
  
Kick it on dotnet-kicks.de  

Beschreibung:

Dieses Snippet macht die Rechtschreibprüfung von Microsoft Word für eigene Anwendungen direkt nutzbar.
Microsoft Word 2000 oder höher muss installiert sein.

Die Anwendung ist denkbar einfach:

1. Variable für Word-Instanz anlegen

object wordApp;

2. Microsoft Word im Hintergund starten

wordApp=SpellChecker.StartWinword();

3. Text auf Rechtschreibfehler prüfen

IList wrongWords=SpellChecker.CheckSpelling("Hallo Welt!",wordApp);

4. Microsoft Word schließen

SpellChecker.QuitWinword(wordApp);

Die Funktion CheckSpelling gibt eine String-Liste zurück, die alle falsch geschriebenen Wörter des Textes enthält.

Eine Visual Studio 2005-Projektmappe mit Beispiel-Applikation gibts unter: http://www.mycsharp.de/wbb2/thread.php?postid=203053#post203053


Abgelegt unter: Rechtschreibprüfung, spell check, spelling, Word, Winword, Office.



C#
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;            
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.