Home : Connecting to a Running MS Office Application
Q30397 - HOWTO: Connecting to a Running MS Office Application

Connecting to a Running MS Office Application

 

When you instanciate a COM object, a new COM object always gets created. Whether this happens within an existing EXE, or whether a new EXE is launched, depends on how the COM object's class factory registers itself (MULTIPLEUSE or SINGLEUSE). So the Java client can not influence whether a new EXE is launched -- it is up to the COM component implementation.

This can be seen with MS Excel -- creating a new instance of Excel.Application always launches a new instance of EXCEL.EXE, whereas creating a new instance of Excel.Worksheet will reuse an existing EXCEL.EXE process.

Native Mode

J-Integra provides a getActiveObject method in the generated Java proxy corresponding to a COM coclass. This method can be used to attach to an existing COM object instance -- this uses native code. You can only connect to a running object in this way if the object registers itself in the Running Object Table (a COM system construct) -- not all objects do this. For more information on connecting to running COM objects in native mode, see the related article.

// This sample code connects to a running instance of Internet Explorer
InternetExplorer app = null;
app = new InternetExplorer(InternetExplorer.getActiveObject());

DCOM Mode

The following example demonstrates how to connect to an existing (already running) instance of MSExcel or MSWord. Please note that the following code for connecting to an existing EXCEL.EXE process does not work in native mode because creating a new Excel.Worksheet fails in native mode (this is not due to a bug in J-Integra as this does not work from VB either). However, connecting to an existing instance of MSWord using the example below works in both DCOM and native mode.

// This connects to Excel
public static void main(java.lang.String[] args) {
  // Note: This code doesn't work in native mode!
  try {
    excel.Worksheet wks = new excel.Worksheet();
    Object oTmp = wks.getPropertyByName("Application");
    excel.Application app = new excel.Application(oTmp);
    app.quit();
    System.err.println("Success!");
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    com.linar.jintegra.Cleaner.releaseAll();
  }
}

 
// This connects to Word
public static void main(java.lang.String[] args) {
  // Note: This works in both DCOM mode and Native mode!
  try {
    word.Document myDoc = new word.Document();
    Object oTmp = myDoc.getParent();
    word.Application app = new word.Application(oTmp);
    app.quit(new Boolean(false), null, null);
    System.err.println("Success!");
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    com.linar.jintegra.Cleaner.releaseAll();
  }
}
Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 6/23/2006.
Last Modified on 7/11/2006.
Last Modified by No Author Name Available!.
Article has been viewed 7656 times.
Rated 6 out of 10 based on 15 votes.
Print Article
Email Article