我是初學C#,在公司寫一個郵件發送程式時,需要把簡體轉換成繁體。

可是按照VS2005中自帶的轉換不是內碼轉換,最后使用了Word2003中的物件進行了編程,代碼非常簡單,希望哪里不妥直接指出。別忘了加個word引用,我的版本是word object library11.0

 1        using Word;
 2
 3            Object template = Type.Missing;
 4            Object newTemplate = Type.Missing;
 5            Object documentType = Type.Missing;
 6            Object visible = Type.Missing;
 7            Microsoft.Office.Interop.Word.Application aWord = null;
 8            Document doc = null;
 9            Object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
10            Object originalFormat = Type.Missing;
11            Object routeDocument = Type.Missing;
12
13            string sAsInputtemp = asInput;
14            try
15            {
16                aWord = new Microsoft.Office.Interop.Word.Application();
17                doc = aWord.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
18                aWord.Selection.TypeText(asInput);
19                //繁体文字转换成為简体文字,逐詞转换
20                aWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC, truetrue);
21                aWord.ActiveDocument.Select();
22                sAsInputtemp = aWord.Selection.Text;
23            }

24            catch (COMException ex) throw ex; }
25            finally
26            {
27                doc.Close(ref saveChanges,
28              ref originalFormat, ref routeDocument);
29                aWord.Quit(ref saveChanges,
30              ref originalFormat, ref routeDocument);
31            }

上面是關鍵代碼,sAsInputtemp 是已經轉換好的文字信息。大家可以隨意修改。

里面的監視剪貼板還沒有寫上去,代碼也很簡單!

Code