1

When calling a external .exe file from vba, like this:

Sub RunEXE()
Dim wsh As Object
SetCurrentDirectory "\\path\" 'Set Directory
Set wsh = VBA.CreateObject("program.exe ""\\path\argument""", WindowStyle:=1, waitonreturn:=True)

Is it possible to close error windows directly from VBA?

The code calls the program and correctly runs the .exe and produces all of the data that is useful to me, but to get the data back to my sheet I have to okay this window (since waitonreturn=true):

enter image description here

The violation appears to be something I can't impact from my VBA code, but it is not impacting the section of the .exe I need in order to do the calculations required by my code. Instead of delving into the world of memory access violation of an external piece of software, I am hoping to side step the problem by okaying it direct from the VBA code.

If I click okay on that error message everything is perfect as far as my code is concerned. I don't need the .exe to save the files causing the error, and I already have the data I need.

Is it possible to dismiss an error window from external software from my excel-VBA code?

NOTE: The error comes from the .exe program, not from excel itself.

Petrichor
  • 975
  • 1
  • 9
  • 22
  • 2
    I wouldn't say this is the best way to go about it, but how about using sendkeys to send a click on OK, this way you wouldn't see the prompt, and your code would automatically press the OK button? – Xabier Dec 12 '17 at 11:12
  • Is the error consistent in in its appearance (Does it pop up every time)? – JustinCoplin Dec 12 '17 at 11:13
  • @JustinCoplin Yes, the error is identical every time. The VBA code calls the .exe multiple times to automate some calculations and for every row of calculations this error appears. The addresses are identical every time. – Petrichor Dec 12 '17 at 11:16
  • @Xabier - To use SendKeys would I not need to change waitonreturn to false? Or can I include it as an argument when calling the .exe? I haven't used sendkeys before. – Petrichor Dec 12 '17 at 11:18
  • @Petrichor I doubt you would need to change anything, just add the sendkeys below your code and it will send the Enter key after you run your program, but be aware that you might have to include some wait time such as application.wait... to make sure you give the program time to respond and prompt you with the error... – Xabier Dec 12 '17 at 11:26

1 Answers1

2

This is a simple example of sendkeys, which will open notepad and then send an Enter key:

Sub SendEnterToNotepad()
    With Application
        Shell "Notepad.exe", 3
        SendKeys "{ENTER}"
    End With
End Sub
Xabier
  • 7,587
  • 1
  • 8
  • 20
  • Perfect, thanks. I have also included AppActivate to ensure sending keys to the correct program as people have noted on some other examples: https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/appactivate-statement – Petrichor Dec 12 '17 at 11:28