JACOB替换WORD中的字符串

//查找是否存在字符串,若存在则把要查找的字符串设置好。
	public static boolean find(Dispatch selection,String text)
	{
		Dispatch find=Dispatch.call(selection, "Find").toDispatch();
		Dispatch.put(find, "Text", text);//设置要查找的文本
		Dispatch.put(find,"Forward","True");//向前找
		Dispatch.put(find,"Format","True");//设置格式
		Dispatch.put(find,"Matchcase","True");//大小写匹配
		Dispatch.put(find,"MatchWholeWord","True");//全字匹配
		return Dispatch.call(find,"Execute").getBoolean();//查找并选中
	}
	
	//全文替换,即文章中所有的字符串都被替换,去掉while则替换一次.
	public static void wordFindReplace(String wordpath,String oldtext,String newtext)
	{
		ActiveXComponent word=new ActiveXComponent("Word.Application");
		word.setProperty("Visible",new Variant(false));//设置word不可见
		Dispatch docs=word.getProperty("Documents").toDispatch();
		Dispatch doc=Dispatch.call(docs, "Open",wordpath).toDispatch();//打开word文件
		Dispatch  selection=Dispatch.get(word,"Selection").toDispatch();//选定范围或者插入点
		
		while(find(selection,oldtext))//全文替换
		{
			Dispatch.put(selection,"Text",newtext);
			Dispatch.call(selection, "MoveRight");
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值