Problematic Call to *GetObject* hanging the application

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,596
Office Version
  1. 2016
Platform
  1. Windows
Hi all

If you call GetObject on a workbook opened in another excel instance ( workbook opened on the same pc or on a remote one) you get a pointer to that workbook ... This works fine . However, if the second workbook happens to be in edit mode or has a modal dialogbox on display (such as the Data Validation dialog) at the time the call to GetObject is made, the calling application hangs and all user interaction becomes impossible until the other workbook comes out of edit mode or the dialogbox is closed.

So, in order to prevent this problem from happening, it would be useful to check beforehand if the called workbook is in an edit mode or is displaying a dialog .. Anybody has an idea how to do that ? or maybe another workaround ?

Regards.
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Off the top of my head... open a third workbook in another instance of Excel which just issues a GetObject to the second workbook and then closes, then wait for a while and see if it managed to close successfully. If that third workbook managed to close, your second workbook wasn't in edit mode and didn't have a modal dialogbox on display. If the third workbook didn't manage to close, it must be hanging, so I guess you'd have to force it to close (if that's possible... you might have to kill that instance of Excel) and then... what? Alert the user that the action on the second workbook needs to be completed? Or wait for whoever was using it to finish what they're doing and try again?

As I said, just a suggestion...
 
Upvote 0
Thanks Ruddles for the answer.

Yes, I had thought of issuing a GetObject to the second workbook from an outide process which I guess would solve the application hanging problem (Although I haven't tested that yet) but I thought I would ask in case someone could think of a more direct method .
 
Last edited:
Upvote 0
[FONT=&quot]Classic VB has the handy Property OLEServerBusyTimeout that permits setting a timeout for an automation request .. I guess this would have been ideal in this scenario if only there was a similar property in VBA

Has anybody come up against the problem described in this thread before when trying to send an automation request from excel or from another office application ?

I have done a search web but not much has come up on the subject... Maybe there is some under-the hood api solution somewhere.


[/FONT]
 
Upvote 0
Ok, after some Win api digging, I think, I have this sorted out - hopefully .
The needed API was CoRegisterMessageFilter

I have wrapped the api in a custom GetObject Sub which I have named Safe_GetObject for easy use as follows :

Code:
Option Explicit

#If VBA7 Then
    Declare PtrSafe Function CoRegisterMessageFilter Lib "OLE32.DLL" (ByVal lFilterIn As Long, ByRef lPreviousFilter As Long) As Long
#Else
    Declare Function CoRegisterMessageFilter Lib "OLE32.DLL" (ByVal lFilterIn As Long, ByRef lPreviousFilter As Long) As Long
#End If


Sub Safe_GetObject(ByRef Obj As Object, Optional ByVal PathName As String, Optional ByVal Class As String)

    Const DBG_EXCEPTION_NOT_HANDLED = &H80010001
    Dim lMsgFilter As Long
    Dim sName As String
    Dim sErrMsg As String
    
    
    On Error GoTo ErrHandler
    Call CoRegisterMessageFilter(0, lMsgFilter)
    Select Case True
        Case Len(PathName) = 0
            Set Obj = GetObject(, Class)
        Case Len(Class) = 0
            Set Obj = GetObject(PathName)
    End Select
    sName = Obj.Name
    Call CoRegisterMessageFilter(lMsgFilter, lMsgFilter)
    Exit Sub
    
ErrHandler:
    sErrMsg = Err.Description
    If Err = DBG_EXCEPTION_NOT_HANDLED Then
        sErrMsg = Err.Description & vbLf & vbLf & "The Server Application may be Busy or in an Disabled state."
        sErrMsg = sErrMsg & vbLf & "Try again later."
        Set Obj = Nothing
    End If
    MsgBox sErrMsg, vbCritical, "Error " & Err & " (&H" & Hex$(Err) & ")"
End Sub

The Server Object is returned in the first ByRef argument of the Safe_GetObject Sub .

Below is a test macro :
Code:
Sub Test()

    Dim oRemoteWorkbook As Workbook
    
    Call Safe_GetObject(oRemoteWorkbook, "C:\Test\Callee.xls")
    If Not oRemoteWorkbook Is Nothing Then
        MsgBox "Connected to :  '" & oRemoteWorkbook.FullName & "  '", vbInformation, "Successful connection"
[B][COLOR=#006400]        'Continue code here ..........[/COLOR][/B]
    End If
End Sub

Now, if the Callee workbook (opened in another instance of excel) is in edit mode or has a modal dialogbox displayed at the time of the automation request when you run the Test Macro , the application no longer hangs... Instead it raises a user freindly error message and continues.
 
Upvote 0

Forum statistics

Threads
1,214,628
Messages
6,120,618
Members
448,973
Latest member
ChristineC

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top