[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Checking To See Is Excel Is Currently Running

Posted on 2004-11-01
4
Medium Priority
?
444 Views
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?





0
Comment
Question by:RipTide
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Assisted Solution

by:michaelstoffel
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.
0
 

Author Comment

by:RipTide
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.

Thanks!
0
 
LVL 33

Accepted Solution

by:
sajuks earned 300 total points
ID: 12469832
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
 

Author Comment

by:RipTide
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*
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

656 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