public static void main(String[] args) {// 启动Word,生成一个ActivexComponent对象ActiveXComponent app = new ActiveXComponent("Word.Application");Map mapBookMark = new HashMap();mapBookMark.put("name","123");// 要转换的word文件String inFi

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 08:27:04

public static void main(String[] args) {// 启动Word,生成一个ActivexComponent对象ActiveXComponent app = new ActiveXComponent("Word.Application");Map mapBookMark = new HashMap();mapBookMark.put("name","123");// 要转换的word文件String inFi
public static void main(String[] args) {
// 启动Word,生成一个ActivexComponent对象
ActiveXComponent app = new ActiveXComponent("Word.Application");
Map mapBookMark = new HashMap();
mapBookMark.put("name","123");
// 要转换的word文件
String inFile = "d:\\1.doc";
// 要报存的目标文件
String tpFile = "d:\\2.doc";
try {
app.setProperty("Visible",new Variant(true));
// 打开word文件
Dispatch docs = (Dispatch) Dispatch.get(app,"Documents")
.toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { inFile,new Variant(false),
new Variant(true) },new int[1]).toDispatch();
Dispatch activeDocument = app.getProperty("ActiveDocument")
.toDispatch();
Dispatch bookMarks = app.call(activeDocument,"Bookmarks")
.toDispatch();
// 标签替换
Iterator it = mapBookMark.keySet().iterator();
while (it.hasNext()) {
String bookMarkKey = (String) it.next();
String bookMarkNewValue = (String) mapBookMark.get(bookMarkKey);
boolean bookMarkExist1 = Dispatch.call(bookMarks,"Exists",
bookMarkKey).toBoolean();
if (bookMarkExist1 == true) {
Dispatch rangeItem = Dispatch.call(bookMarks,"Item",
bookMarkKey).toDispatch();
// System.out.println("range item!");
Dispatch range = Dispatch.call(rangeItem,"Range")
.toDispatch();
// System.out.println("range ");
// 取标签的值
String bookMarkOldValue = Dispatch.get(range,"Text")
.toString();
if (bookMarkNewValue = null) {
Dispatch.put(range,"Text",new Variant(
bookMarkNewValue));
}
} else {
}
}
// 保存文件
Dispatch.invoke(doc,"SaveAs",Dispatch.Method,new Object[] {
tpFile,new Variant(0) },new int[1]);
Dispatch.call(doc,"Close",new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭window
app.invoke("Quit",new Variant[] {});
}
}
--------------
Dispatch docs = (Dispatch) Dispatch.get(app,"Documents").toDispatch(); 总是报错!

public static void main(String[] args) {// 启动Word,生成一个ActivexComponent对象ActiveXComponent app = new ActiveXComponent("Word.Application");Map mapBookMark = new HashMap();mapBookMark.put("name","123");// 要转换的word文件String inFi
报错?报的什么错,编译错误?运行错误?什么都不讲.楼主,我看你也是百度知道六级用户乐,回答了人家很多java问题,想必也是有不少java经验的,对吧,怎么连提个问题也不知道说说清楚.
ActiveXComponent这个东西,我这里是肯定没得,所以我也不拿去编译了.但如果你碰到的是编译错误的话,把错误信息贴上来,那么即使没有那些类也能看出问题吧