How can I change the active MS Word window with JACOB?
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");
精彩评论