I have been tracking this issue for several days, so I decided to post it here to help others with the same issue, as well as to learn more about the cause. I simplified the problem code for the two modules of the class at the end of this post.
The script is basically simplified: two modules of the class, a parent and a child, where Child implements Parent. Somewhere in Parent there is a line TypeOf Me Is Childwhere it Mecan be any object.
From my point of view, when a string is TypeOf...Iscompiled into P-code (Debug> Compile or a method call) and saved in a file (.xlsm or .xlsb), this leads to the file not opening properly, the Code will work fine, but when the file is saved, closed and reopened, it gives an error message when opening (or opening VBE), indicating either Invalid data format, or Error accessing file. Network connection may have been lost, and the parent module can no longer be opened, and no VBA can be started (try ?1=1in the Immediate window and he will give the same error).
If the type is marked with TypeName()instead TypeOf...Is, this problem does not appear (this is the solution I used in my project).
Can someone shed light on what exactly is happening here, or at least confirm that I am on the right track in terms of what causes the problem (P-code)?
PS Yes, I know that a parent who knows about the child has poor design, but I was close to the end of a one-time project that was not worth the time for redesign.
Useful links:
Class Modules:
Parent:
Option Explicit
' Class: Parent
' The problem (so far as I can tell):
' When the compiled version of the method below is saved to the file, the file
' will no longer load properly. Upon saving and reopening the file, I get a
' "Invalid data format" error, and the code for this class module can no longer be
' accessed. Furthermore, no VBA code will run after this happens. Try typing "?1=1"
' into the Immediate Window - you'll get another "Invalid data format" window.
' Alternatively, the error will be "Error accessing file. Network connection may
' have been lost." if the code is changed from using "Me" to "tmp" as noted in the
' comments in DoSomething().
' Steps to replicate:
' 1. Debug > Compile VBAProject.
' 2. Save file.
' 3. Close Excel.
' 4. Reopen file (and may need to open VBE).
Public Sub DoSomething()
' The TypeOf...Is statement seems to be what causes the problem.
' Note that checking "Me" isn't the cause of the problem (merely makes
' for shorter demo code); making a "Dim tmp as Object; set tmp = new Collection"
' and checking "TypeOf tmp Is Child" will cause the same problem.
' Also note, changing this to use TypeName() resolves the issue.
' Another note, moving the TypeOf...Is to a "Private Sub DoSomethingElse()" has
' no effect on the issue. Moving it to a new, unrelated class, however, does
' not cause the issue to occur.
If TypeOf Me Is Child Then
Debug.Print "Parent"
End If
End Sub
Child:
Option Explicit
' Class: Child
Implements Parent
Private Sub Parent_DoSomething()
Debug.Print "Child"
End Sub