Checking To See Is Excel Is Currently Running

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?





RipTideAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

michaelstoffelCommented:
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.
0
RipTideAuthor Commented:
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.

Thanks!
0
sajuksCommented:
Check the below code.
li_return_code=iOLE_Excel.ConnectToObject('',excel.application')
choose case li_return_code
case 0
 lb_excel_was_open=true
case -5
 li_return_code=iOLE_Excel.ConnectToObject('',excel.application')
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RipTideAuthor Commented:
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*
li_return_code=lole_ExcelApp_OLE.ConnectToObject('','excel.application')
CHOOSE CASE li_return_code
    case 0 //Excel is open
         lole_ExcelApp_OLE.DisconnectObject()
         messagebox('Multiple Instance Error', 'Please Close All Instances Of Microsoft Excel')
         return
    case -5 //Excel is not open
        li_return_code = lole_ExcelApp_OLE.DisconnectObject()
        li_return_code = lole_ExcelApp_OLE.ConnectToNewObject( 'excel.application' )
        lole_ExcelApp_OLE.Workbooks.Add
        lole_ExcelSheet = lole_ExcelApp_OLE.Application.ActiveWorkbook.WorkSheets[1]
END CHOOSE

.......
//Code futher down below
lole_ExcelApp_OLE.DisconnectObject()
lole_ExcelSheet.DisconnectObject()
destroy lole_ExcelSheet
DESTROY lole_ExcelApp_OLE
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.