jacob操作word,替换书签内容的同时书签也被删除了,有办法保留书签或者插入新的书签吗?
modifyDocByBookmark()方法用于查找doc文档中名为LB_0的书签
并把该书签中的文本内容替换成新内容
请问在替换书签体中的内容时,能否保留LB_0书签,令它不被删除
或者是在新插入的文本内容上加上新的书签?
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public static void modifyDocByBookmark() {
ActiveXComponent app = new ActiveXComponent( "Word.Application ");
String inFile = "D:\\ligy\\my ebooks\\j4office\\testfile\\wordtemplet_src.doc ";
String bookMarkKey = "LB_0 ";
try {
app.setProperty( "Visible ", new Variant(true));
Dispatch documents = app.getProperty( "Documents ").toDispatch();
Dispatch doc = Dispatch.call(documents, "Open ", inFile).toDispatch();
Dispatch activeDocument = app.getProperty( "ActiveDocument ").toDispatch();
Dispatch bookMarks = app.call(activeDocument, "Bookmarks ").toDispatch();
boolean bookMarkExist1 = Dispatch.call(bookMarks, "Exists ",bookMarkKey).toBoolean();
if (bookMarkExist1 == true) {
System.out.println( "已找到书签 "+bookMarkKey);
Dispatch rangeItem = Dispatch.call(bookMarks, "Item ",bookMarkKey).toDispatch();
Dispatch range = Dispatch.call(rangeItem, "Range ").toDispatch();
String bookMarkValue = Dispatch.get(range, "Text ").toString();
System.out.println( "从书签 "+bookMarkKey+ "中抓取的内容如下: "+bookMarkValue);
bookMarkValue = "书签 "+bookMarkKey+ "中的段落内容已经被删除啦,现在你看到的是替换过来的内容哦,嘿嘿! ";
if (bookMarkValue != null) {
Dispatch.put(range, "Text ", new Variant(bookMarkValue));
//Dispatch.put(range, "Bookmarks ", "LB_001 ");
}
} else {
System.out.println( "指定的书签不存在 ");
}
Dispatch.call(doc, "Save ");
Dispatch.call(doc, "Close ", new Variant(true));
doc = null;
if (app != null) {
Dispatch.call(app, "Quit ");
app = null;
}
documents = null;
} catch (Exception e) {
e.printStackTrace();
}
}
------解决方案--------------------关注
------解决方案--------------------有人知道吗?谢谢!