使用Jacob中dispatch.call方法出错,请大神帮忙

Lucky_AllenLiu 2015-10-14 10:57:20
初次使用Jacob,其中Jacob的配置应该是没什么问题了。但是当我使用一个用例时,其中的dispatch.call方法出现问题,而且在网上找到不少代码,都是同样的问题。希望大神们不吝赐教!
下面贴出用例:
import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComException;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
* Submitted to the Jacob SourceForge web site as a sample 3/2005
* <p>
* This sample is BROKEN because it doesn't call quit!
*
* @author Date Created Description Jason Twist 04 Mar 2005 Code opens a locally
* stored Word document and extracts the Built In properties and Custom
* properties from it. This code just gives an intro to JACOB and there
* are sections that could be enhanced
*/
public class WordDocumentProperties {
// Declare word object
private ActiveXComponent objWord;

// Declare Word Properties
private Dispatch custDocprops;

private Dispatch builtInDocProps;

// the doucments object is important in any real app but this demo doesn't
// use it
// private Dispatch documents;

private Dispatch document;

private Dispatch wordObject;

/**
* Empty Constructor
*
*/
public WordDocumentProperties() {
}

/**
* Opens a document
*
* @param filename
*/
public void open(String filename) {
// Instantiate objWord
objWord = new ActiveXComponent("Word.Application");

// Assign a local word object
wordObject = objWord.getObject();

// Create a Dispatch Parameter to hide the document that is opened
Dispatch.put(wordObject, "Visible", new Variant(false));

// Instantiate the Documents Property
Dispatch documents = objWord.getProperty("Documents").toDispatch();

// Open a word document, Current Active Document
document = Dispatch.call(documents, "Open", filename).toDispatch();
}

/**
* Creates an instance of the VBA CustomDocumentProperties property
*
*/
public void selectCustomDocumentProperitiesMode() {
// Create CustomDocumentProperties and BuiltInDocumentProperties
// properties
custDocprops = Dispatch.get(document, "CustomDocumentProperties")
.toDispatch();
}

/**
* Creates an instance of the VBA BuiltInDocumentProperties property
*
*/
public void selectBuiltinPropertiesMode() {
// Create CustomDocumentProperties and BuiltInDocumentProperties
// properties
builtInDocProps = Dispatch.get(document, "BuiltInDocumentProperties")
.toDispatch();
}

/**
* Closes a document
*
*/
public void close() {
// Close object
Dispatch.call(document, "Close");
}

/**
* Custom Property Name is passed in
*
* @param cusPropName
* @return String - Custom property value
*/
public String getCustomProperty(String cusPropName) {
try {
cusPropName = Dispatch.call(custDocprops, "Item", cusPropName)
.toString();
} catch (ComException e) {
// Do nothing
cusPropName = null;
}

return cusPropName;
}

/**
* Built In Property Name is passed in
*
* @param builtInPropName
* @return String - Built in property value
*/
public String getBuiltInProperty(String builtInPropName) {
try {
builtInPropName = Dispatch.call(builtInDocProps, "Item",
builtInPropName).toString();
} catch (ComException e) {
// Do nothing
builtInPropName = null;
}

return builtInPropName;
}

/**
* simple main program that gets some properties and prints them out
*
* @param args
*/
public static void main(String[] args) {
try {
// Instantiate the class
WordDocumentProperties jacTest = new WordDocumentProperties();

// Open the word doc
File doc = new File(
"samples/com/jacob/samples/office/TestDocument.doc");
jacTest.open(doc.getAbsolutePath());

// Set Custom Properties
jacTest.selectCustomDocumentProperitiesMode();

// Set Built In Properties
jacTest.selectBuiltinPropertiesMode();

// Get custom Property Value
String custValue = jacTest.getCustomProperty("Information Source");

// Get built in prroperty Property Value
String builtInValue = jacTest.getBuiltInProperty("Author");

// Close Word Doc
jacTest.close();

// Output data
System.out.println("Document Val One: " + custValue);
System.out.println("Document Author: " + builtInValue);

} catch (Exception e) {
System.out.println(e);
}
}
}

出错的地方我已经用粗体红色标识了!谢谢大家!
...全文
2736 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
清澈@Cherry 2018-07-04
  • 打赏
  • 举报
回复
应该是使用jacob的版本问题,可以这样修改,经测试在1.5的版本成功运行:
// 作为htm格式保存文件
Object[] objct = { docFile };
Dispatch.call(doc, "Close", objct);
  • 打赏
  • 举报
回复
这样改 Object [] objct={filename}; document = Dispatch.call(documents, "Open", objct).toDispatch();
lizexing1 2016-09-08
  • 打赏
  • 举报
回复 1
我的call方法报错:The method call(Dispatch, String, Object) in the type Dispatch is not applicable for the arguments (Object, String, String),我导入别人的项目,在别人那好好的,在我这就不行,无语了 Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),"FileSaveAs", savePath); 我这是这么写的
x1367442335 2016-05-02
  • 打赏
  • 举报
回复
参数要转成variant类型
dracularking 2015-12-07
  • 打赏
  • 举报
回复
String的话也不用类型转换,或者在某些需要转换的情形下,实际执行有错吗?
思而后行 2015-12-06
  • 打赏
  • 举报
回复
楼主搞定没啊,我也被这个问题郁闷几天了。。。。
Lucky_AllenLiu 2015-10-14
  • 打赏
  • 举报
回复
这个我都放了,只是call方法出错了,这是call的定义,参数列表第三行是object类型,但是在操作代码中第三列要么是文件路径即String类型,要么是variant类型。我不知道是要类型转换还是我的Jacob这个包不对,已经郁闷好多天了。
tony4geek 2015-10-14
  • 打赏
  • 举报
回复
出什么异常 了, 要拷贝dll 到system 下面好像,以前做过忘了。

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧