Programmatically check VBA compiles as part of release procedure?

This should surely be a very simple one!  Either "not possible" or "yes, in one line of code..."

I maintain a number of VBA projects in Excel.
I have a release procedure in each i.e. to copy the Dev Workbook over to UAT before copying to Live.

I would love to have one line in the release macro that says (in pseudocode)

If ThisWorkbook.VBACompiles() Then
    ThisWorkbook.SaveCopyAs {uat_path}
Else
    {report_compile_error}
End If

Open in new window


The closest I can get is
ThisWorkbook.VBAProject.MakeCompiledFile()

Open in new window


but seems this is legacy or even spurious functionality because the online help says it
"Causes the current project to be written as a DLL. The DLL name is specified by the BuildFileName property."

I'm not trying to create a DLL.

All I want to do is replicate the VB Editor Debug menu's Compile command (which is really just a syntax checker) programmatically and confirm all is well before releasing the update.

Anyone out there know how to do this?
doowellAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
stavros41Connect With a Mentor Commented:
I don't think that's possible as if the code fails then it will break out of the code and therefore would not be able to log the compilation error.

You could put a boolean variable at the end of the code so that if it is set to true then you know the code has run correctly?
0
 
doowellAuthor Commented:
and please no one suggest SendKeys !
0
 
Rory ArchibaldCommented:
The best I think you can do is use:


    On Error Resume Next
    Application.VBE.CommandBars.FindControl(ID:=578).Execute
On Error Goto 0



before your save code. If the compile fails, the rest of the code won't run.


0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
doowellAuthor Commented:
Not quite - it'll run if the module in which the release macro resides compiles.
Says nothing about the other modules alas.
So if you have a VBA project of N modules, the release macro will run even if the other N-1 modules won't compile.
0
 
Rory ArchibaldCommented:
Ah, should have checked multiple modules.
Then I think the answer is no, since no error is returned, in spite of the compile messages.
0
 
calacucciaCommented:
Haven't had time to test it, this launches the manual Compile VBAProject button.

Sub CompileProject()
'
     With Application
           .ScreenUpdating = False
           '
           On Error Resume Next
           With .VBE
                 'need the VBE window open for this one
                 .MainWindow.Visible = True
                 .CommandBars("Menu Bar").Controls("Debug") _
                             .Controls("Compile VBAProject").Execute
                 .MainWindow.Visible = False
           End With
           '
           .ScreenUpdating = True
     End With
     '
End Sub
0
 
calacucciaCommented:
Oh, I see rorya already posted this method ... and got stuck as well.
0
 
doowellAuthor Commented:
All, excellent contributions.
Invoking the Compile menu option does run the compiler but it doesn't return the result of it (e.g. True for success).
The return value of the .Execute method seems to be empty - when I get invoke it...
Because ... hen in Debugging mode, the "Compile..." option is greyed-out under the Debug menu.  Trying to execute its command therefore throws an error.
And even when not debugging, the menu option remains greyed out until I edit the code in a way that causes the project to be reset.
So we're making progress but not yet there.
Any further thoughts?!
0
 
doowellAuthor Commented:
Sorry few typos - corrections here


All, excellent contributions.
Invoking the Compile menu option does run the compiler but it doesn't return the result of it (e.g. True for success).
The return value of the .Execute method seems to be empty - when I get *TO* invoke it...
Because ... *when* in Debugging mode, the "Compile..." option is greyed-out under the Debug menu.  Trying to execute its command therefore throws an error.
And even when not debugging, the menu option remains greyed out until I edit the code in a way that causes the project to be reset.
So we're making progress but not yet there.
Any further thoughts?!
0
 
calacucciaCommented:
I thought of checking the dll to be empty if code does not compile aftter running a MakedCompileFile command, turns out this command is only avaialble for specific project for Office Developer package users, not for plain Excel VBA.

I guess you already use Explicit (which will prevent some of the errrors raised by the compile you look for)?
0
 
doowellAuthor Commented:
@calacuccia

Thanks again.  Not sure I understand your comment
"I guess you already use Explicit (which will prevent some of the errrors raised by the compile you look for)?"
0
 
doowellAuthor Commented:
Ah right sorry yes I get you now.  yes, Option Explicit is part of my gospel ;o)
0
 
Rory ArchibaldCommented:
If the menu item is disabled, then you are SOL.
The only thing I can think of currently would be to execute the button, hook the resulting message box, if any, and return a variable using the hook procedure. Just need to figure out the implementation. :)


0
 
calacucciaCommented:
Option Explicit in each code window on top
0
 
calacucciaCommented:
Forget it, does not change anything, it only raises variable errors upon compiling.
0
 
doowellAuthor Commented:
I have a feeling I'm going to have to admit defeat on this one.

It would be SO NICE to be able to ensure all VBA code compiles prior to release.

Having said that I understand VBA will soon have C# alongside it.  It's about time that VBA was updated to VB.Net.
0
 
calacucciaCommented:
I've been trying to find Compile Error handling documentation or topics, but every thread I read goes towards handling the Compile Error cause and prevent it from happening, rather than trapping it.

I get the same feeling as you, except catching the message Box from a compile Error, but have no clue right now how to do this.
0
 
doowellAuthor Commented:
My word -- I have rarely come across anything that this illustrious community cannot solve!
Let's hope some inspiration strikes.
Meanwhile it's still quite interesting seeing how the hive mind here is working.
0
 
Rory ArchibaldCommented:
Currently my attempts to hook the message box are stuck as I can't get the right ThreadID (it seems not to be the one running the VBA). May have to resort to using a timer to see if a messagebox comes up after executing the button and then set a variable from there.
0
 
doowellAuthor Commented:
Wow I really appreciate the collective determination here.
Seems like this is going to be a real bi_ch to solve.
I can live without this but on the other hand will greatly appreciate a working answer!
Best wishes to all (and don't work too hard).
0
 
doowellAuthor Commented:
Yes but the problem is no solution has appeared - what should I do?
0
 
doowellAuthor Commented:
OK having read the link you provide, I will wait another week or so for a solution otherwise I will ask for the question to be deleted
0
 
doowellAuthor Commented:
As per the Help Page, I am following this guidance:


Leave a request in the Community Support topic area to delete the question (no solution)
Before you ask the Moderators to help you in this circumstance, take the time to post a message in your question stating your intention to have the question deleted or closed, and make sure you have responded to all of the Experts' comments. Then post a question in the Community Support Zone asking the Moderators to delete your question. Remember to post a link to the original question in your request. If there are no comments in the question, one of the Moderators will delete the question. If there are comments, the Moderator will post notice of your request, and will give the participants four days to object. You may be required to post your reason for asking for the deletion, and objections will be taken into account.
0
 
Rory ArchibaldCommented:
It would seem to me that your question was answered (initially by stavros41) it's just that the answer was no. That is still a valid answer though.
(there's no object button on the mobile site, but you can consider this one)
0
 
doowellAuthor Commented:
Sorry thought I had closed this but obviously not...
0
 
doowellAuthor Commented:
Seems no solution is possible so have awarded points to the person who pointed this out first
0
All Courses

From novice to tech pro — start learning today.