2009-08-31 6 views
3

J'ai une application de formulaire Windows interne dans laquelle j'aimerais utiliser la vérification orthographique. Tout le monde a installé Office 2007 alors je ne devrais pas avoir de problème, mais j'ai de la difficulté à obtenir ceci pour fonctionner pleinement.Implémenter l'orthographe Word dans l'application Windows Form

Voici ce que j'ai:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Word = Microsoft.Office.Interop.Word; 
using System.Reflection; 

namespace Refraction.Spelling 
{ 
    public static class SpellCheckers 
    { 
     public static string CheckSpelling(string text) 
     { 
      Word.Application app = new Word.Application(); 
object nullobj = Missing.Value; 
       object template = Missing.Value; 
       object newTemplate = Missing.Value; 
       object documentType = Missing.Value; 
       object visible = true; 
       object optional = Missing.Value; 
      object savechanges = false; 
      Word._Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible); 

     doc.Words.First.InsertBefore(text); 
     Word.ProofreadingErrors errors = doc.SpellingErrors; 

     var ecount = errors.Count; 
     doc.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); 
     object first = 0; 
     object last = doc.Characters.Count - 1; 
     var results = doc.Range(ref first, ref last).Text; 
     doc.Close(ref savechanges, ref nullobj, ref nullobj); 
     app.Quit(ref savechanges, ref nullobj, ref nullobj); 

     return results; 
    } 
} 

} 

J'utilise cette façon:

memDirectionsToAddress.Text = SpellCheckers.CheckSpelling(memDirectionsToAddress.Text); 

Maintenant, ce qui ouvre avec succès le dialogue Vérification orthographique de Word et détecte les mots mal orthographiés , mais je ne peux pas pour apporter les corrections dans l'application WinForm .

En outre, il laisse ce «Shell» d'un document Word ouvert avec le texte corrigé. Comment ne pas montrer cela ou au moins le faire disparaître?

Deux choses:

  • d'abord, bien que la "coquille" le ferme clignote à chaque fois. Des solutions à cela?
  • Deuxièmement, le dialogue de vérification orthographique n'apparaît pas sur , que puis-je régler sur pour corriger cela?

Merci

+0

Question supplémentaire: Y a-t-il une raison de ne PAS rendre STATIC? –

+0

Si vous ne le rendez pas statique, vous pouvez conserver des références à un document spécifique et/ou au mot application. Cela vous aiderait à éviter les coûts de démarrage d'une nouvelle application ou d'un nouveau document. (Vous pouvez conserver un document ouvert mais invisible, en jouant avec la propriété Visible ou l'application et en démarrant toujours une nouvelle application.) –

+0

le problème est que si je ne fais pas un 'app.Close()' à la fin puis un Shell de Word reste ouvert .... –

Répondre

1

Les prochaines étapes seraient:

  1. Tirez le texte corrigé en arrière sur le document.
  2. Ferme le document. (Si un seul document est ouvert dans Word, vous pouvez vouloir fermer ou masquer l'application Word.)
  3. Renvoyez le texte corrigé à la fonction d'appel.

Plus d'infos:

+0

Merci, donc il corrige le texte dans le document "caché" doc et puis j'ai besoin de saisir cela. Des conseils sur la façon dont je récupère le texte? –

+0

Utilisez l'objet de sélection. Faites un "tout sélectionner", puis lisez le texte hors de la sélection. –

+0

voir mon commentaire sur la Question pour une réponse COMPLETE –

0

J'ai un vieux script pour ce où les fonctionnalités requises sont toutes appelées donc pas ref DLL sont nécessaires pour mot lui-même:

internal class SpellChecker 
{ 
    public SpellChecker() 
    { 
    } 

    public static string Check(string text) 
    { 
     bool flag; 
     string str = text; 
     flag = (text == null ? true : !(text != "")); 
     bool flag1 = flag; 
     if (!flag1) 
     { 
      Type typeFromProgID = Type.GetTypeFromProgID("Word.Application"); 
      object obj = Activator.CreateInstance(typeFromProgID); 
      object[] objArray = new object[1]; 
      object obj1 = typeFromProgID.InvokeMember("Documents", BindingFlags.GetProperty, null, obj, null); 
      object obj2 = obj1.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, obj1, null); 
      object obj3 = obj2.GetType().InvokeMember("ActiveWindow", BindingFlags.GetProperty, null, obj2, null); 
      objArray[0] = 0; 
      obj3.GetType().InvokeMember("WindowState", BindingFlags.SetProperty, null, obj3, objArray); 
      object[] objArray1 = new object[] { -2000, -2000 }; 
      obj.GetType().InvokeMember("Move", BindingFlags.InvokeMethod, null, obj, objArray1); 
      objArray[0] = "Spell Check"; 
      obj3.GetType().InvokeMember("Caption", BindingFlags.SetProperty, null, obj3, objArray); 
      object obj4 = typeFromProgID.InvokeMember("Selection", BindingFlags.GetProperty, null, obj, null); 
      objArray[0] = text; 
      obj4.GetType().InvokeMember("TypeText", BindingFlags.InvokeMethod, null, obj4, objArray); 
      objArray[0] = 6; 
      obj4.GetType().InvokeMember("HomeKey", BindingFlags.InvokeMethod, null, obj4, objArray); 
      object obj5 = obj2.GetType().InvokeMember("SpellingErrors", BindingFlags.GetProperty, null, obj2, null); 
      int num = (int)obj5.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, obj5, null); 
      flag1 = num <= 0; 
      if (flag1) 
      { 
       System.Windows.Forms.MessageBox.Show("Spellcheck is correct"); 
      } 
      else 
      { 
       obj3.GetType().InvokeMember("Activate", BindingFlags.InvokeMethod, null, obj3, null); 
       objArray1 = new object[] { -5000, -5000 }; 
       obj.GetType().InvokeMember("Move", BindingFlags.InvokeMethod, null, obj, objArray1); 
       objArray[0] = true; 
       obj.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, obj, objArray); 
       obj2.GetType().InvokeMember("CheckSpelling", BindingFlags.InvokeMethod, null, obj2, null); 
       objArray[0] = true; 
       obj2.GetType().InvokeMember("Saved", BindingFlags.SetProperty, null, obj2, objArray); 
       object obj6 = obj2.GetType().InvokeMember("Content", BindingFlags.GetProperty, null, obj2, null); 
       str = obj6.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, obj6, null).ToString(); 
       str = str.Trim(); 
      } 
      flag1 = obj == null; 
      if (!flag1) 
      { 
       objArray[0] = true; 
       obj2.GetType().InvokeMember("Saved", BindingFlags.SetProperty, null, obj2, objArray); 
       obj.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, obj, null); 
      } 
     } 
     string str1 = str; 
     return str1; 
    } 
} 

Juste le nourrir le texte et il reviendra avec toutes les corrections que vous approuvez.

Questions connexes