For larger VBA projects (40,000+ lines of code) I cannot properly use interfaces because the Application (I mainly use Excel) will crash quite often. Apparently, this is because the code cannot remain compiled (from my understanding VBA code gets compiled to P-code which is later interpreted). I mainly get this behavior when the VBA Project is password protected.
The Debug/Compile menu is almost never "greyed out" when I open the hosting document:
This article describes the same behavior. Go to section 2.3
For example:
IClass
interface:
Option Explicit
Public Property Get SomeProperty() As Double
End Property
Class1
:
Option Explicit
Implements IClass
Private Property Get IClass_SomeProperty() As Double
IClass_SomeProperty = 0
End Property
Code in standard module:
Option Explicit
Sub TestInterface()
Dim obj As IClass
Set obj = New Class1
Debug.Print obj.SomeProperty 'Crashes here on large projects only
End Sub
As expected the Debug.Print obj.SomeProperty
line works fine and prints 0 in the Immediate window if the project is small. However, on a large project the application crashes when this line is called. The IClass_SomeProperty
is not reached (logging to a file clarifies this).
As in the above-mentioned article, there are ways to temporarily avoid the issue:
- Recompiling solves the issue (not always) but the crash could occur on the very next time the document is opened, or it might keep things going for a few days (presuming file is opened every day)
- When option 1 is not working, deactivating all
Implements
statements in the entire project using the Find/Replace window, then compiling and then reactivating the statements back. Again, this can work for a few hours or a few days but inevitably a crash will occur in the near future
Since the VBA Project is password protected and because many people are using these VBA-capable documents (Workbooks in my case), applying the temporary fixes does not help at all.
The only way that I've found to avoid the crashes and still get the benefits of the interfaces is to use conditional compilation. Basically, I use interfaces only for development and then switch to late-binding for production. Obviously, this comes with a lot of headaches.
The example above becomes:
Class1
:
Option Explicit
#Const USE_INTERFACES = True
#If USE_INTERFACES Then
Implements IClass
#End If
Private Property Get IClass_SomeProperty() As Double
IClass_SomeProperty = Me.SomeProperty
End Property
Public Property Get SomeProperty() As Double
SomeProperty = 0
End Property
Notice that all interface methods must be duplicated and made public so that late-binding is an option.
Code in standard module:
Option Explicit
#Const USE_INTERFACES = True
Sub TestInterface()
#If USE_INTERFACES Then
Dim obj As IClass
#Else
Dim obj As Object
#End If
Set obj = New Class1
Debug.Print obj.SomeProperty
End Sub
When developing new features, I follow these steps:
- turn
#Const USE_INTERFACES = True
for all occurrences using Find/Replace - add new features
- turn
#Const USE_INTERFACES = False
so that code runs on late-binding and doesn't crash
I've been experiencing this bug for at least 3 years. I would obviously avoid the conditional compilation workaround if I could.
Is there a way to keep the VBA Project compiled (let's say running a procedure when opening the document) without access to the VBA project object model? It is not an option for me to have the Trust access to the VBA project object model turned on.
I appreciate this bug is not easy to re-create unless you happen to have a large VBA project at hand.
EDIT 1
A nice point raised by @PEH in the comments: this issue is applicable for both xlsm
and xlsb
files (Excel).
.xlam
started to exceed 1,5 Mb. It would crash during compilation or if it compiled it would throw errors where it worked before and then crash during debugging (classes sometimes would also get corrupted, but other files formats never solved the issue). I did not realize it has to do with interfaces. My solution was to pull the project apart into multiple files. Currently my core project is around 14.000 lines and it relies on up to 6 other files that have another 12.000 lines together and it works. I figured it is just a VBA limitation.