Compile through VBT giving errors - excel

In the Excel VBA, when I give Compile VBAproject, it doesn't show any error and the compilation is successful.
But I was trying to achieve the same using VBA command like below,
Public Function CheckForCompilerErrors()
On Error GoTo errpt
....Excel.VBE.CommandBars.FindControl(id:=578).Execute
Exit Function
errpt:
MsgBox Err.Description
End Function
Err.Description shows 'Object variable or With block variable not set'.
Before executing this method, I made sure that Compile option is not disabled.
But when I do the same through Compile->VBAProject no issues. How to identify from where the error is thrown when compiled through VBA. It is not highlighting to any code. Just displays the error message.

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 find if any compile errors exists due to missing DLL reference?

We have developed a program in Excel VBA.
Say for instance we are having a compiler problem in one of the functions (Debug->Compile VBAProject gives some type mismatch error).
Without clicking that option, is there any way after the program load, we can intimate the user by showing a message like "You have compiler error in your test program, please click Debug->Compile VBAProject and fix the compiler errors"?
Actual problem is, a program (Excel VBA) is having compiler error, but the user doesn't know it. He just started running the program which results in failure.
So after the program load, without manually clicking Debug->Compile, after the excel is launched, it should throw a pop-up saying "you are having compiler errors, Do Debug->Compile VBAProject and fix it".
The actual error is "Compiler error: User-defined type not defined".
As part of our software installation, we are installing certain DLLs in the customer machine. The customer can add those DLLs as reference and edit their program.
As the Excel program is having a compiler error, "OnProgramLoaded interpose" function is not called. So the code inside that function is not executed. The user has no clue that this code is not called.
Additional information: Earlier we have the "Compile On Demand" checkbox as checked. So even though there is a compiler error in the program, the "OnProgramLoaded interpose" function is getting called. Now for different reason, we have removed the check box from "Compile On Demand", so what happens now is, since there is compiler error, it is not executing the other functions too.
So what we are trying to do is, as soon as the program is launched, we need to give the message to user that the program is having compiler errors. Fix it and restart the program.
You can check, if your desired VBA references are already enabled:
Private Sub CheckVBAReferences()
Dim dict As Object
Dim oRef As Object
Dim i As Long
Set dict = CreateObject("Scripting.Dictionary")
For Each oRef In ThisWorkbook.vbProject.References
dict.Add oRef.Description & ", " & oRef.fullpath, oRef.GUID
Debug.Print oRef.Description & ", " & oRef.fullpath
Next oRef
If Not dict.Exists("Microsoft Excel 16.0 Object Library, " & _
"C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE") Then
MsgBox "Please add a VBA reference to ..."
' or set it according to following link
End If
dict.RemoveAll
Set dict = Nothing
End Sub
And then you may set a missing reference according to this answer.

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.

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