50,526
社区成员
发帖
与我相关
我的任务
分享
/**
* 在书签处插入内容
* 替换书签内容后,重新标注书签,并设置书签内容为所有人可编辑
*/
public boolean changeLabelValue(String labelName, String labelValue) {
Dispatch bookMarks = Dispatch.call(doc, "Bookmarks").toDispatch();
boolean isExist = Dispatch.call(bookMarks, "Exists", labelName).getBoolean();
if (isExist == true) {
Dispatch item = Dispatch.call(bookMarks, "Item", labelName).toDispatch();
Dispatch range = Dispatch.get(item, "Range").toDispatch();
Dispatch.call(range, "Select");//选中书签所在区域
Dispatch.put(selection, "Text", labelValue);//插入书签内容
Dispatch.call(bookMarks, "Add", labelName, selection);//将填充的内容设置为书签(不这样操作书签会消失)
Dispatch Edit = Dispatch.call(selection, "Editors").toDispatch();//选中书签内容处的编辑属性
Dispatch.call(Edit, "Add", new Variant(-1));//设置为每个人可编辑,其中参数-1表示wdEditorEveryone,还有其他参数可在word com开发帮助中查找wdEditorType参数
moveStart();//移动光标至文档开头
return true;
}else {
System.out.println("当前书签不存在!");
return false;
}
}
/**
* 给文档加上保护
*/
public void setProtected(String pswd) throws Exception{
String protectionType = Dispatch.get(doc, "ProtectionType").toString();
if(protectionType.equals("-1")){
Dispatch.call(doc, "Protect", new Variant(3), new Variant(true), pswd);
}
}
/**
* 把插入点移动到文件首位置
*/
public void moveStart() {
if (selection == null)
selection = Dispatch.get(word, "Selection").toDispatch();
Dispatch.call(selection, "HomeKey", new Variant(6));
}