2009-08-31 2 views
3

맞춤법 검사를 사용하려는 사내 Windows 양식 응용 프로그램이 있습니다. Office 2007이 설치되어있어 모두 거기에 문제가 없어야하지만 문제가 발생합니다. 이것은 완전히 작동합니다.Windows 양식 응용 프로그램에서 Word 맞춤법 검사 구현

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; 
    } 
} 

} 

내가 지금처럼 이것을 사용 :

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

지금이 성공적으로 말씀에서 맞춤법 검사 대화 상자를 팝업 및 맞춤법이 틀린 단어 을 감지하지만 내가 얻을 수 없다 여기

내가 가진 무엇 그것은 WinForm 애플 리케이션에서 수정을 할 .

또한 워드 문서의이 "쉘"은 수정 된 텍스트로 열어 둡니다. 그걸 보여주지 않거나 적어도 없어지지는 않는가?

두 가지 :

  • 첫째, "쉘이"그것을 깜박 매번을 닫습니다하지만. 그 어떤 해결책을 ?
  • 둘째, 맞춤법 검사 대화 상자에서 이 TOP에 나타나지 않습니다. 무엇을 으로 수정하면됩니까?

감사

+0

기타 질문 :이 기능을 고정시키지 않는 이유가 있습니까? –

+0

정적으로 만들지 않으면 특정 문서 및/또는 응용 프로그램이라는 단어에 대한 참조를 유지할 수 있습니다. 그러면 새로운 앱이나 문서의 초기 비용을 피할 수 있습니다. (Visible 속성이나 앱을 가지고 놀고 항상 새로운 앱을 시작하면 문서를 열어도 보이지 않게 유지할 수 있습니다.) –

+0

만약 내가'app.Close()'를하지 않으면 Word의 껍데기가 끝나면 끝납니다. –

답변

1

다음 단계가 될 것이다는 :

  1. 문서에서 다시 수정 된 텍스트를 당깁니다.
  2. 문서를 닫습니다. Word에서 문서를 하나만 열면 Word 응용 프로그램을 닫거나 숨길 수 있습니다.
  3. 수정 된 텍스트를 호출 함수에 반환하십시오.

상세 정보 :

+0

고마워, 실제로 "숨겨진"단어 doc의 텍스트를 수정 한 다음 그 텍스트를 가져와야합니다. 텍스트를 다시 가져 오는 방법에 대한 힌트가 있습니까? –

+0

선택 개체를 사용합니다. "모두 선택"을 한 다음 선택 영역에서 텍스트를 읽습니다. –

+0

내 코멘트보기 전체 질문에 대한 답변 –

0

내가 필요한 기능을 모든 단어 자체가 필요합니다 그래서 DLL 심판을 호출하지 않습니다 이것에 대한 오래된 스크립트가 :

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; 
    } 
} 

그냥 텍스트를 입력하면 승인 된 수정 사항이 반환됩니다.