Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Programmatically check VBA compiles as part of release procedure?

Posted on 2010-08-24
28
Medium Priority
?
1,543 Views
Last Modified: 2013-11-25
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?
0
Comment
Question by:doowell
  • 14
  • 6
  • 5
  • +1
26 Comments
 

Author Comment

by:doowell
ID: 33509425
and please no one suggest SendKeys !
0
 
LVL 6

Accepted Solution

by:
stavros41 earned 2000 total points
ID: 33509468
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33509552
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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:doowell
ID: 33509575
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33509609
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
 
LVL 17

Expert Comment

by:calacuccia
ID: 33509669
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
 
LVL 17

Expert Comment

by:calacuccia
ID: 33509678
Oh, I see rorya already posted this method ... and got stuck as well.
0
 

Author Comment

by:doowell
ID: 33509801
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
 

Author Comment

by:doowell
ID: 33509820
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
 
LVL 17

Expert Comment

by:calacuccia
ID: 33509865
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
 

Author Comment

by:doowell
ID: 33509888
@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
 

Author Comment

by:doowell
ID: 33509900
Ah right sorry yes I get you now.  yes, Option Explicit is part of my gospel ;o)
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33509907
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
 
LVL 17

Expert Comment

by:calacuccia
ID: 33509923
Option Explicit in each code window on top
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33509932
Forget it, does not change anything, it only raises variable errors upon compiling.
0
 

Author Comment

by:doowell
ID: 33509941
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
 
LVL 17

Expert Comment

by:calacuccia
ID: 33510670
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
 

Author Comment

by:doowell
ID: 33510683
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33510773
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
 

Author Comment

by:doowell
ID: 33511143
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
 

Author Comment

by:doowell
ID: 33693320
Yes but the problem is no solution has appeared - what should I do?
0
 

Author Comment

by:doowell
ID: 33693329
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
 

Author Comment

by:doowell
ID: 33745456
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33746081
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
 

Author Comment

by:doowell
ID: 33796851
Sorry thought I had closed this but obviously not...
0
 

Author Closing Comment

by:doowell
ID: 33796862
Seems no solution is possible so have awarded points to the person who pointed this out first
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question