none
Open Excel in new instance, run macro and check for error RRS feed

  • Question

  • Hello,

    is there a way to achieve this?

    1. Open new instance of Excel and open an Excel file
    2. Run a macro in this file (in the new instance)
    3. Wait for a successful end
    4. In case of (unhandeled) error/break .. do something: e.g. Close the new instance file and report an error
    5. continue with other code in the initial instance of Excel

    Warm regards

    Jakub Dušek

    Friday, June 8, 2018 9:12 AM

Answers

  • Hello jakub dusek,

    As far as I know, it is not possible to handle the error since it occurs in another separate application instance. I think you could consider about opening the workbook in current application instance for handling the error or handle the error in your opened workbook's code.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by jakub dusek Friday, June 15, 2018 8:22 AM
    Thursday, June 14, 2018 8:55 AM

All replies

  • hello,

    a little example, hope it helps:

    Public Function openExcel(ByVal strOrigPath As String
                                 Optional strVBAMacro As String = "") As Boolean

    On Error GoTo err

        Dim ea  As object

        Set ea = CreateObject("Excel.Application")
        
       
        Call ea.Workbooks.Open(strOrigPath)  'openen van een bestaande excelsheet
       
        ea.Visible = True  'beter debug mogelijkheden

        If strVBAMacro <> "" Then
            ea.Run (strVBAMacro)
            
            
        End If
        
      
        ea.Workbooks.Item(strFileName).Saved = True
        ea.Workbooks.Item(strFileName).Close
        ea.Quit
      
        Set ea = Nothing
        openExcel = True

       
        
    Exit Function:
    err:
        
        If (Not ea Is Nothing) Then
          ea.Workbooks.Item(strFileName).Close False
          ea.Workbooks.Close
          ea.Quit
        End If
         Set ea = Nothing
       'your err hndler
    End Function

    Friday, June 8, 2018 10:53 AM
  • Hello,

    not working.

    In the second - opened - excel/instance, i have this macro to simulate error:

    Sub boom()
    
    Debug.Print sdfgfsdg / 0
    
    End Sub

    When I use your function to run it, it stops on error/debug pop-up in the second excel/instance and your function stops processing until I close that error/debug pop-up message.

    Wednesday, June 13, 2018 10:29 AM
  • what exact error do you get?

    Also, try to put the macro you want to call in a Public Function, in a seperate module

    Wednesday, June 13, 2018 11:08 AM
  • Hello jakub dusek,

    As far as I know, it is not possible to handle the error since it occurs in another separate application instance. I think you could consider about opening the workbook in current application instance for handling the error or handle the error in your opened workbook's code.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by jakub dusek Friday, June 15, 2018 8:22 AM
    Thursday, June 14, 2018 8:55 AM