1

I am currently working with this great tool Jacob, but I get something very strange. Reading a MS Word file, I tokenize the document and, for each token I get, I would like to have also the page and line number. THE strange thing is that it works only if I start MSWord myself and get the instance from Jacob. If Java starts a new instance, I cannot get these information. The other point is that I get many information from the file, that work great, so it seems as if the only trouble is dealing with the Information property.

Parts of my code are :

ActiveXComponent myApplication = ActiveXComponent.connectToActiveInstance("Word.Application");
if (myApplication == null) {
  myApplication = new ActiveXComponent("Word.Application");
}
Dispatch myDocuments = myApplication.getProperty("Documents").toDispatch();
Dispatch myDocument = getOpenedDocument(myDocuments, pFilePath);

....

Dispatch myParagraphs = Dispatch.call(myDocument, "Paragraphs");
Dispatch myParagraph = Dispatch.call(myParagraphs, "Item", new Variant(1)).toDispatch();
Dispatch myParagraphRange = Dispatch.get(myParagraph, "Range").toDispatch();

Then for exemple when I try :

myText = Dispatch.get(myParagraphRange, "Text").toString();

I get the paragraph content. But with :

int myPageNumber = Dispatch.call(myParagraphRange, "Information", 3).getInt();

IF and ONLY if MSWord isn't started by myself, I get :

com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Information
Description: Le serveur a généré une exception.

It seems that the "Information" property is having problems with Word ???

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Rolintocour
  • 2,934
  • 4
  • 32
  • 63

1 Answers1

0

Finally find what's wrong with my code. For an unknown ready, for the property Information to be get, the document should not be force to be not visible. When you open your document with

Dispatch.call(lObjDocuments, "Open", myPath, new Variant(false)....");

my last parameter concerned the visibility of the Word process. Just removed it and it works (thanks Microsoft)

Rolintocour
  • 2,934
  • 4
  • 32
  • 63