Alternate message when VBA can't open file - excel

I have a VBA script that when the user executes it, it opens and writes new data to another file on a Network share drive. Occasionally the script can't access this file to write new data to it because someone else is simultaneously trying to open it (via read only through a VBA script that they have).
This particular file opening error is not caught in the error handling code and a box comes up and asks the user to End or Debug. Is there a way to bring up an alternate popup box that says something like "Could not process request. Please wait a few seconds and try again."

yes. put in error handling around the file opening code. like this:
on error goto FileError
'file opening code
on error goto 0
exit sub
FileError:
msgbox("Could not process request. Please wait a few seconds and try again")
basically, the OnError tells everything after that statement to do whatever you specify in the event of an error. in this case I've put goto FileError so if an error is encountered anywhere after the onError processing will stop and go to the line marked FileError.
then the OnError goto 0 disables that error handle. that way if other code generates an error it wont be handled by the FileError handle. the other option for onerror is OnError Resume Next which makes the program skip over code that generates an error. so it wont throw the error, and it will just continue processing.
Note the: Exit Sub before the FileError label, that way the code wont go into the FileError label unless it is sent there by an error being generated.

Related

VBA Selenium Chrome Unexpected Alert Error

I am currently using Selenium, VBA Excel, and Chrome to scrape information from a site. Everything works fine until one of the values from my list is not available on the site, then I get a Run-time error '26' UnexpectedAlertOpenError. Error image.
I have added this line Chrome.SwitchToAlert(5).accept to handle the error which works when the value is not available. Unfortunately, adding that line returns a Run-Time error '27' NoAlertPresentError when the value from the list is available. Error image.
The error messages make sense, can't act on what is not there. I need a way to check if there is an alert and if so then quit chrome, else run the rest of the code. I have tried If Chrome.FindElementsByTag("tr") Is Nothing Then Chrome.SwitchToAlert(5).accept else and If Chrome.FindElementsByXPath("//*[contains(text(),'Details')]").Count > 0 Then and other things but nothing seems to work to address the error 26.
Well so based on the VBA selenium library I checked apparently there is no way of checking a count of the alerts already displayed, something we could do for browser windows by using:
driver.Windows.Count 'driver being the variable used to refer to an instance of Chromedriver
So the solution I am using is of wrapping the entire selenium code between error handlers:
On Error Goto Problem:
' Your code over here
''''''
''''''
On Error Goto 0
Exit Sub
Problem:
If InStr(1, Err.Description, "Alert") <> 0 Then
driver.SwitchToAlert.accept 'could also use: driver.SwitchToAlert.dismiss
Resume Next 'resumes code from the next line in code that threw error
End If
This way I believe whenever any sudden unexpected alert pops up, the error handler will accept/dismiss the alert and with 'Resume Next' ensure program moves on from the next line

How to suppress excel compiler error from VB6

I am executing the Excel 'Debug -> Compile VBAProject' from a VBA project like below,
Public Function CheckForCompilerErrors()
On Error GoTo compileerr
ExcelObject.ActiveWorkbook.VBProject.VBE.CommandBars.findcontrol(ID:=578).Execute
Exit Function
compilerErr:
MsgBox "Compilation failed. Give Debug->Compile VBAProject and fix the compilation errors."
End Function
Here after running the .Execute, Excel VBA is throwing the error "Compiler Error: User-defined type not defined.".
But actually I want to suppress this Excel VBA error. That's the reason why I added error handler. But in the above case, it is not going to error handler.
Maybe what I am suspecting is, since the .Execute got successfully called (which invokes Debug->Compile VBAProject) whether there is any error or not, it returns success and not going to error handler.
Is there anyway, I can suppress the Excel VBA compiler error.
When you are sending the click to the Compile button, there will be an error if the button is unavailable, which it is if you just compiled something. That error you can catch and suppress.
If there is a compilation error, you won't be able to catch it this way.

Runtime Error with Workbook Auto Open + MsgBox as response for an user

I was trying to put MsgBox into my code. It should be shown only if call Table.auto_open doesn't work.
In my final document are few of those Call statements and i would like to get only that msgbox, if one or few of Call statements doesn't work.
For Example "Auto_open" will be changed to "auto_op" what naturally won't be possible, because in real sheet it is "Auto_open".
Or in another example that code from "Auto_open" is broken.
I need some help with that. It's seems to be simple, but I think it's not possible to put that "On Error GoTo" code in that place just like that, because Call doesn't give me a real error?
Can someone say me what I'm doing wrong? I tried already all combinations of that Error handling, nothing works.
Private Sub Workbook_Open()
On Error GoTo Error
Call Tabelle1.auto_open
Exit Sub
Error:
MsgBox "Failure"
Resume Next
End Sub
If you are calling a sub that does not exist you will get a Compile Error.
You can check for these errors by going to VBE>Debug>Compile VBAProject (or just try to run the macro)
Compile Errors, much like Syntax Errors, have to be handled before you can run a sub. Thus, these errors cannot be handled with code such as On Error GoTo EH or On Error Resume Next as these are only activated once the sub is actually running.
You can convince yourself of this by producing a common Compile error, or Syntax error, and trying to step through the code (F8). You will notice that the error occurs on your Sub [Name] () line, which indicates that you never actually entered your sub before the error occurred. Thus, you can intuitively see that your error handler will never actually be activated, resulting in an error message being displayed.
Once you have accounted for all Compile/Sytax Errors, you can check out this link, which will explain how you can handle Run Time Errors when you are calling other sub procedures from a sub.

Capture DataFeedConnection errors using vbscript

I have a data feed connection in excel. I'm trying to refresh a data feed connection using vbscript. Here's how I try to refresh the connection.
Set oWorkbook = oExcel.Workbooks.Open('<excel file name/path>')
oWorkbook.Connections(1).Refresh
The total number of records returned by the data feed(OData) is actually huge and the conection isn't stable. Hence, I often get the error stating that "The connection is closed" while trying to refresh the connection from excel manually. A snapshot of the error below. Whenever I get this error, I try refreshing the connection again and this results in successful refresh most of the times.
I want to achieve the same result in vbscript as well. When I refresh the connection using vbscript, I get the same error and my script stops running as its unable to capture and handle the error.
In the documentaion I see that I can capture errors only if its OLEDBError or ODBCError. I'm not able to find the similar ways to capture DataFeedConnection error. Is there any way to capture the DataFeedConnection errors?
Edit: To update you with more information on the vbscript, the script picks up multiple files from the current folder, refresh the first or second connection depends on the file name and then compare certain values before and after refresh to make sure the refresh has happened successfully and then decide saving or closing the file based on the comparison results. The reason for comparison is there are some times refresh brings only partial data.
How about the following, adding an "On Error GoTo" then using a msgbox to display the error description:
Sub Foo()
On Error GoTo WhatHappened
'Your code here
WhatHappened:
MsgBox Err.Description
End Sub

Err.Number not populating for 'Excel Ran Out of Resources' error

I've developed a program using Excel VBA which occasionally causes an 'Excel Ran Out of Resources' error.
Closing the file, reopening, and rerunning the macro always fixes whatever issue created the error. I know that preventing the error in the first place is best practice, but am resigned to believe that it's unavoidable.
So, I would like to alert the user of the error, instead of Excel doing it, and perform some actions once the error has been detected. The problem is that I can't get VBA to recognize the error using the On Error GoTo ErrorHandler routine or the Err.Number property. I never get to the msgbox below:
My code is as follows:
Sub test()
On Error GoTo ErrorHandler
Calculate
ProcedureDone:
Exit Sub
ErrorHandler:
MsgBox "Error", vbOKOnly, "Oops"
Resume ProcedureDone
End Sub
Any insight would be fantastic since I've been searching for several days and haven't been able to find a work around.
I just happened to run across another issue that sounds like yours.
The point from the other thread is that Excel does not treat these application method results as VBA errors. Rather, they are Excel alerts, and they can be suppressed but not trapped in VBA as errors.
The way I interpret this is that when you execute certain application methods from VBA, it does not raise errors that VBA can trap. Rather, Excel interacts with the user as if the user had issued a GUI command. On the other hand, if an application method is designed to interact with VBA (e.g., if it returns a value), then VBA might be able to handle its errors.
This is distinct from the way VBA handles worksheet functions rather than application methods. VBA can intercept errors raised by worksheet functions, as noted in "Error Handling With Worksheet Functions" here.
I realize this does not solve your problem, but it gives you an idea of why.

Resources