jacob自己動生成word文檔目錄


任務目的

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));

源碼
http://download.csdn.net/download/wolf12/9938312


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM