VBA - Method to Detect Compilation Failure

2019-08-16 17:22发布

问题:

I have the following code:

Public Function Compiler()
    On Error GoTo ErrorHandler

    Compiler = "Successfully Compiled"

    Dim compileMe As Object
    Set compileMe = Application.VBE.CommandBars.FindControl(Type:=msoControlButton, ID:=578)

    If compileMe.Enabled Then
        compileMe.Execute
    End If

    Exit Function

ErrorHandler:

    Compiler = "Unable to Compile - " & Err.Description

End Function

It is very similar to the suggestion posted here, and it doesn't work. It you introduce errors into the rest of your application and run this, you will get "Successfully Compiled" every time (after clicking past the error messages).

Is there a way to have the method return "Unable to Compile" if the file cannot be compiled?