0

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?

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
David Rogers
  • 2,601
  • 4
  • 39
  • 84
  • refer https://stackoverflow.com/questions/11560934/when-editing-microsoft-office-vba-how-can-i-disable-the-popup-compile-error-m when tools - options - auto syntax check is Off your code will bring up the popup you require but will not suppress the error since it's a basic syntax check that it does and is out of scope of VBA runtime. – user2648008 Apr 11 '19 at 04:32
  • @user2648008 Certainly, and I'm sure I can check that option to suppress the popup, however that's not really the main issue that's occurring, whether or not that option is enabled doesn't change the fact that this method is unable to detect whether the compilation was successful or not, until I can figure out a way to do that, this method will not work correctly. – David Rogers Apr 11 '19 at 12:55

1 Answers1

0

My solution (returns true/false):

Public Function func_app_compile() As Boolean
   On Error GoTo err_proc
   Dim cmd As Object
   Set cmd = Application.VBE.CommandBars.FindControl(Type:=msoControlButton, id:=578)
   If cmd.Enabled Then cmd.Execute
exit_proc:
   On Error GoTo 0
   DoEvents
   func_app_compile = Not cmd.Enabled
   Exit Function
err_proc:
   Err.Raise vbObjectError - 1000, , "Project failed to compile!"
End Function

Then you can use:

If Not func_app_compile Then End