Checking To See Is Excel Is Currently Running

Posted on 2004-11-01
Medium Priority
Last Modified: 2013-12-26
I need to check to see if Excel is currently running using PB.

VB offers the options of using both CreateObject("Excel.Application") and GetObject(, "Excel.Application").

I can use VB's GetObject to determine if an instance of Excel is running, and if not, then I can use VB's Createobject and be assure that I only have one instance open at that time.

 I am also interested in using an existing instance as the default server to create and attach an OLEObject to it.

Any ideas?

Question by:RipTide
  • 2

Assisted Solution

michaelstoffel earned 75 total points
ID: 12465737
Use ConnectToNewObject.  From PowerBuilder Help:

Creates a new object in the specified server application and associates it with a PowerBuilder OLEObject variable. ConnectToNewObject starts the server application if necessary.

Author Comment

ID: 12466619
Using that already.

I need to determine if an instance of Excel is currntly running.

If ConnectToNewObject does in fact do that, then please provide a brief example of how it would return a value.

According to PB Help, this:

Creates a new object in the specified server application and associates it with a PowerBuilder OLEObject variable. ConnectToNewObject starts the server application if necessary.

It returns:

Integer. Returns 0 if it succeeds and one of the following negative values if an error occurs:

-1  Invalid Call: the argument is the Object property of a control
-2  Class name not found
-3  Object could not be created
-4  Could not connect to object
-9  Other error
-15  MTS is not loaded on this computer
-16  Invalid Call: this function not applicable

If any argument's value is NULL, ConnectToNewObject returns NULL.

LVL 33

Accepted Solution

sajuks earned 300 total points
ID: 12469832
Check the below code.
choose case li_return_code
case 0
case -5
end choose

Basically you need to check the return from ConnectToObject. if it is zero then excel is open
else you need to create an instance

Author Comment

ID: 12496253
Sajuks (and Michael, thanks for your input as well):

Thanks for the lead. The return code -5 was in the list, but the return code descriptions on ConnectToObject() weren't explicit enough and confusing. Again, thanks for your efforts. I've added my solution below for others to use in the event the need should arise.

My solution was two fold.

1. If Excel is open then return code is 0. If not open, then return code is -5.

2. Also, the target window was a response window. As such, any OLE apps remain open until the calling window is also closed which created an inconvenience for the user. Otherwise, if the user exported multiple dw's to Excel, then just as many Excel applications would remain in memory minus the last one that is destroyed when the calling window is closed. Also, PB did not allow for the destruction of the OLE application by way of the code. Calling Destroy failed silently or was overlooked.

I changed the window to a main window. Now, the code allows for checking of existing Excel apps in memory. If it exists, I do a return and throw a msg. Also PB now allows for the destruction of the OLE application by way of the code.

See snippet below! Also, note the difference in the parameters for ConnectToObject and ConnectToNewObject

// *Updated Code*
CHOOSE CASE li_return_code
    case 0 //Excel is open
         messagebox('Multiple Instance Error', 'Please Close All Instances Of Microsoft Excel')
    case -5 //Excel is not open
        li_return_code = lole_ExcelApp_OLE.DisconnectObject()
        li_return_code = lole_ExcelApp_OLE.ConnectToNewObject( 'excel.application' )
        lole_ExcelSheet = lole_ExcelApp_OLE.Application.ActiveWorkbook.WorkSheets[1]

//Code futher down below
destroy lole_ExcelSheet
DESTROY lole_ExcelApp_OLE

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question