jacob去除word修订

jacob去除word里面的修订。

因为本人是做去除表格操作的时候因为当前word文档里面含有修订所以会导致表格删除不成功。
去除word文档里面的修订就3句话:

		Dispatch.put(wordFile,"TrackRevisions",new Variant(false));
        Dispatch.put(wordFile,"PrintRevisions",new Variant(false));
        Dispatch.put(wordFile,"ShowRevisions",new Variant(false));

加了这3句话word文档修订可以去除。

完整代码:

public static void wipeOutShowRevisions(String filePath) throws IOException {
     		 	
	 	Dispatch wordFile = null;

		ActiveXComponent word = null;
	    try{
     
        word=new ActiveXComponent("Word.Application");    
        word.setProperty("Visible", new Variant(false));         
          //获得所有文档对象
        Dispatch documents=word.getProperty("Documents").toDispatch();  
        wordFile=Dispatch.invoke(documents, "Open", Dispatch.Method, new Object[]{
     filePath,new Variant(true),new Variant(false)}, new int[1]).toDispatch();   
        Dispatch.put(wordFile,"TrackRevisions",new Variant(false));
        Dispatch.put(wordFile,"PrintRevisions",new Variant(false));
        Dispatch.put(wordFile,"ShowRevisions",new Variant(false));
		Dispatch.call((Dispatch) Dispatch.call(word, "WordBasic").getDispatch(),"FileSaveAs", filePath);
		/**另存为。更新文档*/
		Dispatch.invoke(wordFile, "SaveAs", Dispatch.Method, new Object[] {
         
				filePath, new Variant(true) }, new int[1]);
	    }finally{
     
        	try{
     
        		if (wordFile != null) {
     					
        			Dispatch.call(wordFile, "Close", false);
				}
        		if (word != null){
     
        			word.invoke("Quit", new Variant[] {
     });
        		}   
        	} catch (Exception e2) {
     
        		
        	}
	    }
	    //如果没有这句话,winword.exe进程将不会关闭
	   
	}

总结:

因为本人之前做着相关业务逻辑时候碰到过这个坑。因为当前文档有修订你对文档做什么操作都会无效的。所以在做操作前把文档修订去除掉就可以了。

我不知道我这个代码写的对不对。如果有问题还请各位在底下多多评论。

你可能感兴趣的:(java)