Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序)
: V, h$ w5 X' n% Z, ? //Add a new word document, Current Active Document ) w& ]2 [) k! n& K |4 \7 ]
Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档 - {0 Y% ?/ P* C( G4 S. S
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容 : |& ^( }4 O5 b% ~& ]# M
Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落
8 g9 K/ g2 k; @/ | Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落 ( }2 ^: C5 j" `* | m* i
int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数
$ i9 ?( P% p8 a // 找到刚输入的段落,设置格式
) Q1 z" N! R2 P& k+ A Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
+ x1 R/ f2 E8 }( s new Variant(paragraphCount)).
: M0 Y9 l3 S; A1 ?* g2 `* i toDispatch(); // 最后一段
$ u' N( \ ~1 p/ x8 R/ P6 i/ W1 [ Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range").
+ S/ Z; u. v7 w7 o) t5 \0 T4 G6 f toDispatch();
8 L: ]2 a9 E& V6 c) d Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch(); 6 b, d) l- Z p/ `
Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体
1 e" j$ N1 f7 G: M/ p) O; K$ k Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体 r1 w8 ]" s g" h
Dispatch.put(font, "Name", new Variant("宋体")); //
; r1 t+ K6 M" Z& Q* s; M Dispatch.put(font, "Size", new Variant(12)); //小四 % t1 B) ^1 Y; x1 p1 n9 v# d
Dispatch.call(document, "SaveAs", new Variant("C:
4 H, k g( G4 V9 D* a6 p abc.doc")); // 保存一个新文档
$ o1 ?0 P, J& w- k; w ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理 7 F* K4 r# I# E2 I
} } |