早教吧作业答案频道 -->其他-->
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...的网友还看了以下:
matlab解中学三角函数方程数学题,不会求大大~~~~~~~~~~[a,b,c,A,B,C]=s 2020-05-14 …
1、已知a,b,c互不相等求2a-b-c/(a-b)(b-c)+2b-c-a/(b-c)(b-a) 2020-05-16 …
分解因式(a-b-c)(a+b-c)-(b-c-a)(b+c-a)正确答案是这个:(a+b-c)( 2020-05-17 …
已知a+b+c=0,试求a^2/(2a^2+bc)+b^2/(2b^2+ac)+c^2/(2c^2 2020-06-11 …
a(b-c)^5+b(c-a)^5+c(a-b)^5分解为(a-b)(b-c)(c-a)L(aa( 2020-07-09 …
设a,b,c都是正数且a+b+c=1,求证:(1+a)(1+b)(1+c)≥8(1-a)(1-b) 2020-07-25 …
(a+b+c)^3-(b+c-a)^3-(c+a-b)^3-(a+b-c)^3=[(a+b+c)^ 2020-08-02 …
要对多个字符进行加双引号,例如输入中国,则加上双引号后是"中""国"staticvoidMain() 2020-11-10 …
C程序中,对的数组有1.合法的数组定义是()。A.chara[]={‘s’,‘t’,‘r’,‘i’, 2020-12-05 …
C语言理论题2下列表达式,错误的是.A.charstr[10];str="string";\x05B 2020-12-23 …