2

Similar to Errors when calling certain Excel VBA macros from C#, I want to implement an error handler that will catch VBA errors and return gracefully.

Basically, I'm opening up an arbitrary macro workbook in Excel, and if the macro fails (i.e. you get a VBA error dialog with "end" and "debug" buttons), I want my application to handle it internally instead of using the VBA's dialogs. This is primarily so that a buggy book won't freeze processing (I'm using a queue of sorts to process multiple workbooks).

I already know about the DisplayAlerts=False property, and On Error Resume Next inserted into each routine, but I don't want to need to inject this into everything (especially since some of these workbooks may already modify the mentioned settings and reset them mid-routine!)

My best idea so far is to write up some sort of process watcher that looks for windows titled "Microsoft Visual Basic" and sendkey an "E" (to click the end button).

I've read in a few places that there's a property called Application.BreakOnVBAError that can be set to False, but it doesn't seem to exist in the interop assembly.

Community
  • 1
  • 1
Tsaukpaetra
  • 579
  • 5
  • 26
  • 2
    VB.NET <> vbscript <> vba – Ňɏssa Pøngjǣrdenlarp Aug 08 '14 at 19:32
  • @Plutonix I'm programming this wrapper in VB.Net, hence the VB.Net tag... This is a VB.Net application that instantiates a COM object of Excel that then calls a VBA Macro. The intent is to handle potential cases of VBA errors inside the VB.Net application. – Tsaukpaetra Aug 08 '14 at 20:17

1 Answers1

0

With the help of pinvoke.net and MWinAPI, I've been able to enumerate all of the windows created under Excel's process ID, which I can then drill down into to discover the elements of the dialog (if it's a dialog such as the Microsoft Visual Basic dialog).

Since it's an automated instance, the main window is hidden so the only thing that should show up are message boxes and the VBA error box, which I can then send keyboard input messages to to End the script when necessary.

Of course, this method never hits the "Debug" button so I should never see the VBE windows, but just in case I handle that too.

Source code available upon request?

Tsaukpaetra
  • 579
  • 5
  • 26
  • Yes would be useful to see if you still have it (or found a better alternative ;) – Greedo Jul 24 '19 at 07:40
  • @Greedo Oof, sorry, I'm not sure if I still have that code actually. It's been a while since I even had Office or Visual Studio installed, actually... – Tsaukpaetra Jul 25 '19 at 06:40
  • 1
    @Greedo You're in luck, I managed to find an old copy of my project. Sadly, no source, but the binary should be decompile-able. https://drive.google.com/drive/folders/1QulkOErWby_rKRDlo09Z51KRnlC_hqda?usp=sharing – Tsaukpaetra Jul 29 '19 at 07:30