如下是一個測試操做word的實例:僅供參考java
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComException; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class JacobTest { // 聲明一個word對象 private ActiveXComponent objWord; // 聲明四個word組件 private Dispatch custDocprops; private Dispatch builtInDocProps; private Dispatch document; private Dispatch wordObject; public JacobTest() { } /** * 打開word文擋 */ public void open(String filename) { // 建立一個word對象 objWord = new ActiveXComponent("Word.Application"); // 爲wordobject組件附值 wordObject = (Dispatch) (objWord.getObject()); // 改了這裏 // 生成一個只讀方式的word文擋組件 Dispatch.put(wordObject, "Visible", new Variant(false)); // 獲取文擋屬性 Dispatch documents = objWord.getProperty("Documents").toDispatch(); // 打開激活文擋 document = Dispatch.call(documents, "Open", filename).toDispatch(); } public void selectCustomDocumentProperitiesMode() { custDocprops = Dispatch.get(document, "CustomDocumentProperties").toDispatch(); } public void selectBuiltinPropertiesMode() { builtInDocProps = Dispatch.get(document, "BuiltInDocumentProperties").toDispatch(); } /** * 關閉文擋 */ public void close() { Dispatch.call(document, "Close"); } public String getCustomProperty(String cusPropName) { try { cusPropName = Dispatch.call((Dispatch)custDocprops, "Item",cusPropName).toString(); } catch (ComException e) { cusPropName = null; } return cusPropName; } public String getBuiltInProperty(String builtInPropName) { try { builtInPropName = Dispatch.call((Dispatch)builtInDocProps,"Item", builtInPropName).toString(); } catch (ComException e) { builtInPropName = null; } return builtInPropName; } public static void main(String[] args) { try { JacobTest jacTest = new JacobTest(); jacTest.open("c:/s.doc"); jacTest.selectCustomDocumentProperitiesMode(); jacTest.selectBuiltinPropertiesMode(); String custValue = jacTest.getCustomProperty("Information Source"); String builtInValue = jacTest.getBuiltInProperty("Author"); jacTest.close(); System.out.println("Document Val One:" + custValue); System.out.println("Document Author:" + builtInValue); } catch (Exception e) { System.out.println(e); } } }
jacob1.1.5-M4.zip下載地址:http://115.com/file/beeqxg90#jacob-1.15-M4.zip(附帶一個操做word示例代碼和一個操做文檔)c#
結束語: 第一次在開源中國發文,若有錯誤之處,你們能夠盡情留言進行探討。
服務器