早教吧作业答案频道 -->其他-->
publicstaticvoidmain(String[]args){//启动Word,生成一个ActivexComponent对象ActiveXComponentapp=newActiveXComponent("Word.Application");MapmapBookMark=newHashMap();mapBookMark.put("name","123");//要转换的word文件StringinFi
题目详情
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(); 总是报错!
// 启动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(); 总是报错!
▼优质解答
答案和解析
报错?报的什么错,编译错误?运行错误?什么都不讲.楼主,我看你也是百度知道六级用户乐,回答了人家很多java问题,想必也是有不少java经验的,对吧,怎么连提个问题也不知道说说清楚.
ActiveXComponent这个东西,我这里是肯定没得,所以我也不拿去编译了.但如果你碰到的是编译错误的话,把错误信息贴上来,那么即使没有那些类也能看出问题吧
ActiveXComponent这个东西,我这里是肯定没得,所以我也不拿去编译了.但如果你碰到的是编译错误的话,把错误信息贴上来,那么即使没有那些类也能看出问题吧
看了 publicstaticvo...的网友还看了以下:
如何用复数表示这些量?设A,B,C为三角形三顶点的复数,H,I,O,G,P分别表示垂心、内心、外心 2020-06-18 …
1.r,h,n,y,e2.c,e,s,c,n,i,e3.p,1,a,p,e4.r,a,t5.p,o 2020-07-09 …
英语单词填空1.时间状语:d-r-n-2.场所:b-s-s-o-f-r--e-a-t-e-t3.教 2020-07-14 …
#includemain(){char*p[]={"BOOL","OPK","H","SP"};i 2020-07-23 …
选出下面各项中字音有误的一项:A泥淖nào羞赧nǎn忸怩ní泥墙nìB睥睨pìnì亲昵nì酿造nià 2020-11-07 …
选出下面各项中字音有误的一项:A.炮烙páo澎湃péngpài胚胎pēi便笺qiānB.毗邻pí媲美 2020-11-07 …
选出下面各项中字音有误的一项:A炮烙páo澎湃péngpài胚胎pēi便笺qiānB毗邻pí媲美pì 2020-12-02 …
下列各项,注音有误的一项是()A.庖páo庖厨胚pēi胚胎tāi澎péng澎湃pàiB.抨pēng抨 2020-12-14 …
给下列多音字组词。熬áo()āo()间jiān()jiàn()差chā()chà()chāi()cī 2020-12-24 …
英文单词分类填空动物类1.O()t()p()s2.k()t()e()3.l()b()t()r4.a( 2021-02-05 …