0

I am using the JACOB library. 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?

Pops
  • 30,199
  • 37
  • 136
  • 151
Alur
  • 1
  • 1
  • This is not an answer to your question, but you might want to consider com4j. Many people have reported memory leakage using jacob (my own experience with it was marred with constant JVM crashes): http://stackoverflow.com/questions/2066318/com4j-versus-jacob-to-call-com-methods-from-java – jt. Mar 22 '11 at 14:29

2 Answers2

2

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.

Nico
  • 21
  • 2
1

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");
Sotiris
  • 38,986
  • 11
  • 53
  • 85
Herb
  • 11
  • 1