JAva呼び出しWordにおけるFindの実現方法(jacob)

1914 ワード


		ActiveXComponent activeXApp = null;
		try {
			activeXApp = new ActiveXComponent("Word.Application");

			activeXApp.setProperty("Visible", new Variant(false));

			Dispatch docs = activeXApp.getProperty("Documents").toDispatch();

			String docFilePath = "";
			String result = "";

			Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, 
					new Object[] { docFilePath, new Variant(false), 
					new Variant(false) }, new int[1]).toDispatch();

			Dispatch selection = Dispatch.get(activeXApp, "Selection").toDispatch();
			Dispatch find = Dispatch.call(selection, "Find").toDispatch();
			Dispatch.put(find, "Text", "[[]*:*[]]");
			Dispatch.put(find, "Forward", "True");
			Dispatch.put(find, "Format", "False");
			Dispatch.put(find, "MatchCase", "False");
			Dispatch.put(find, "MatchWholeWord", "False");

			Dispatch.put(find, "MatchByte", "False");
			Dispatch.put(find, "MatchAllWordForms", "False");
			Dispatch.put(find, "MatchSoundsLike", "False");
			Dispatch.put(find, "MatchFuzzy", "False");
			Dispatch.put(find, "MatchWildcards", "True");

			while (Dispatch.call(find, "Execute").getBoolean()) {
				result = Dispatch.get(selection, "Text").toString();
			}

			Dispatch.call(doc, "Close", new Variant(false));
		} catch (Exception e) {
			throw e;
		} finally {
			if (activeXApp != null) {
				activeXApp.invoke("Quit", new Variant[] {});
			}
			ComThread.Release();
		}