任务目的
1自动生成word文档目录。
用例测试操作步骤
在一个word文档的第二页填写占位符:
{目录}保存。调用程序读取目标文档,自动根据标题生成目录到{目录}位置。
效果
关键代码
insertText("目 录");
Dispatch alignment = Dispatch.get(selection, "ParagraphFormat")
.toDispatch(); // 行列格式化需要的对象
Dispatch.put(alignment, "Alignment", "1"); // (1:置中 2:靠右 3:靠左)
// insertNewParagraph();
moveRight(1);
Dispatch range = Dispatch.get(this.selection, "RANGE").toDispatch();
Dispatch fields = Dispatch.call(this.selection, "FIELDS").toDispatch();
Variant call = Dispatch.call(fields,
"ADD",
range,
new Variant(-1),
new Variant("TOC"),
new Variant(true));
Dispatch tablesOfContents = Dispatch.call(doc, "TablesOfContents").toDispatch();// 整个目录区域
// 整个目录
Dispatch tableOfContents = Dispatch.call(tablesOfContents, "Item", new Variant(1)).toDispatch();
// 拿到整个目录的范围
Dispatch tableOfContentsRange = Dispatch.get(tableOfContents, "Range").toDispatch();
// // 取消选中,应该就是移动光标
Dispatch format = Dispatch.get(tableOfContentsRange, "ParagraphFormat").toDispatch();
// // 设置段落格式为首行缩进2个字符
Dispatch.put(format, "CharacterUnitLeftIndent", new Variant(1));