Well when the VBE
is closed upon the first opening of the application, there is no ActiveCodePane
, you can check this in a conditional upon loading your form:
If (Application.VBE.ActiveCodePane Is Nothing) Then MsgBox "ActiveCodePane is Nothing"
The VBE
exists and properties and methods can be used, but there is no ActiveCodePane
which is why you're receiving the null reference exception. Just opening the VBE
will still produce your error if you closed all CodePanes
before saving and closing previously (unless a module exists for some reason). You must the explicitly open a CodePane
, to set the 'ActiveCodePane' property.
This makes sense. What is it that you're trying to access via the ActiveCodePane property? Perhaps I can help find a way around?
Edit
Presumably, as you develop this Form and associated Modules, you'll know what they're called, and would be able to use a different method than the ActiveCodePane
, such as that which @Bas Verlaat mentioned. Alternatively, you can loop through each code pane in the active VBProject and try and match on a name or something:
Option Compare Database
Option Explicit
Private vbProj As VBIDE.VBProject
Private vbComp As VBIDE.VBComponent
Private vbMod As VBIDE.CodeModule
Private Sub Command0_Click()
Set vbProj = Application.VBE.ActiveVBProject
For Each vbComp In vbProj.VBComponents
MsgBox vbComp.CodeModule
Next
End Sub
Workbook_Open()
handler and the...Activate
solution from Bas Verlaat below solved it nicely. Strangely theErr
object was not available on this error so debugging was quite weird. – Meakem