Jacob操作Word书签

Jacob操作Word书签

ActiveXComponent word = new ActiveXComponent("Word.Application");
 word.setProperty("Visible", 
new Variant(false));
/***************************************************************************
     * 
删除书签
     * 
     * 
@param mark  书签名
     * 
@param info  可替换
     * 
@return
     */

    
public boolean deleteBookMark(String markKey, String info) throws Exception{
        Dispatch activeDocument = word.getProperty("ActiveDocument")
                .toDispatch();
        Dispatch bookMarks = word.call(activeDocument, "Bookmarks")
                .toDispatch();
        
boolean isExists = word.call(bookMarks, "Exists", markKey)
                .toBoolean();
        
if (isExists) {
            
            Dispatch n = Dispatch.call(bookMarks, "Item", markKey).toDispatch();
            Dispatch.call(n, "Delete");
            
            
return true;
        } 
        
return false;
    }
/***************************************************************************
     * 
根据书签插入数据
     * 
     * 
@param bookMarkKey 书签名
     * 
@param info  插入的数据
     * 
@return
     */

  
    
public boolean intoValueBookMark(String bookMarkKey, String info) throws Exception{
               
        Dispatch activeDocument = word.getProperty("ActiveDocument")
                .toDispatch();
        Dispatch bookMarks = word.call(activeDocument, "Bookmarks")
                .toDispatch();
        
boolean bookMarkExist = word.call(bookMarks, "Exists", bookMarkKey)
                .toBoolean();
        
if (bookMarkExist) {
            
            Dispatch rangeItem = Dispatch.call(bookMarks, "Item", bookMarkKey)
                    .toDispatch();
            Dispatch range = Dispatch.call(rangeItem, "Range").toDispatch();
            Dispatch.put(range, "Text", 
new Variant(info));
            
return true;
        } 
        
return false;
    }

 

你可能感兴趣的:(Jacob操作Word书签)