Solved

Checking To See Is Excel Is Currently Running

Posted on 2004-11-01
385 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
Question by:RipTide
    4 Comments
     
    LVL 5

    Assisted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    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: …
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now