C# 利用 Word 作簡繁轉換

C# 利用 Word 作簡繁轉換

加入參考 Microsoft Word 12.0 Object Library

命名空間 using Microsoft.Office.Interop.Word;

  1. public string TC2SC( string strSource)
  2. {
  3. string strResult;
  4. _Application objWord = new Microsoft.Office.Interop.Word.Application ( );
  5. object t = Missing.Value;
  6. object nt = Missing.Value;
  7. object dt = Missing.Value;
  8. object v = true;
  9. Document objDocument = objWord.Documents.Add ( ref t, ref nt, ref dt, ref v);
  10. objWord.Selection.TypeText (strSource);
  11. objWord.Selection.Range.TCSCConverter (WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC, true, true );
  12. objWord.ActiveDocument.Select ( );
  13. strResult = objWord.Selection.Text;
  14. object sc = 0;
  15. object of = Missing.Value;
  16. object rd = Missing.Value;
  17. objWord.Quit ( ref sc, ref of, ref rd);
  18. objDocument = null;
  19. objWord = null;
  20. return strResult;
  21. }
  22. public string SC2TC( string strSource)
  23. {
  24. string strResult;
  25. _Application objWord = new Microsoft.Office.Interop.Word.Application ( );
  26. object t = Missing.Value;
  27. object nt = Missing.Value;
  28. object dt = Missing.Value;
  29. object v = true;
  30. Document objDocument = objWord.Documents.Add ( ref t, ref nt, ref dt, ref v);
  31. objWord.Selection.TypeText (strSource);
  32. objWord.Selection.Range.TCSCConverter (WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC, true, true );
  33. objWord.ActiveDocument.Select ( );
  34. strResult = objWord.Selection.Text;
  35. object sc = 0;
  36. object of = Missing.Value;
  37. object rd = Missing.Value;
  38. objWord.Quit ( ref sc, ref of, ref rd);
  39. objDocument = null;
  40. objWord = null;
  41. return strResult;
  42. }