开发者

How can I change the active MS Word window with JACOB?

开发者 https://www.devze.com 2023-02-19 03:28 出处:网络
I am using the JACOB lib开发者_运维百科rary. I can create a new instance of activeXComponent(\"Word.Application\") and then open some file. How can I get the instance of a file that I\'ve already open

I am using the JACOB lib开发者_运维百科rary. I can create a new instance of activeXComponent("Word.Application") and then open some file. How can I get the instance of a file that I've already opened?

In MS Word macros I can use Windows("name.doc").Activate, where "name.doc" represents the file name. How can this be done in JACOB?


You can connect to the running Word-Application:

val com_wordApp: ActiveXComponent = ActiveXComponent.connectToActiveInstance("Word.Application")

This is scala code. I am not sure, but in Java it should be

ActiveXComponent com_wordApp = ActiveXComponent.connectToActiveInstance("Word.Application")

Then you can access the Active Document, e.g. to get the Bookmarks:

val bookMarks: Dispatch = Dispatch.call(com_wordApp.getProperty("ActiveDocument").toDispatch, "Bookmarks").toDispatch

Or you can get all documents:

val com_documents: AnyRef = com_wordApp.getProperty("Documents").toDispatch

Sorry for the scala code. I needed a lot of time to get this working, so I hope, I could help anybody.


First you open document,

Dispatch document1 = Dispatch.call(documents, "Open", "name.doc").toDispatch();

then you may open other documents, and you want to control document1 for "name.doc" again.

You just need to call

Dispatch.call(document1, "Activate");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号