Can't define VBA objects as their proper type!

Discussion created by AlistairFrith on Mar 17, 2011
Latest reply on Mar 18, 2011 by dhollebeek

Any ideas why I get a "Runtime error 13: Type Mismatch" on this code if any of the VBA automation objects are defined as their proper type instead of 'Object'?



' This module contains functions to control the import of other modules. The reason for this
' is that we have a number of displays that need to share identical code. Therefore, the
' code modules will be stored outsideof the display files and imported if they are newer than
' the version currently in the display.
' This means that any change to the module should be exported so that other displays may
' benefit from the improvements and the world remains balanced and at peace.

' Returns whether the module has been imported since its source file was last saved
Public Function ModuleSynchronised(modulename As String) As Boolean
    Dim VBAEditor As Object
    Dim VBProj As Object 'VBIDE.VBProject
    Dim VBComp As Object
    Dim CodeMod As VBIDE.CodeModule
    Set VBAEditor = Application.VBE
    Set VBProj = Application.VBE.VBProjects.Item(1)
    Set VBComp = Nothing
    On Error Resume Next
    Set VBComp = VBProj.VBComponents(modulename)
    If (Err.Number <> 0) Then
        ModuleSynchronised = False
        Exit Function
    End If
    On Error GoTo 0
    Set CodeMod = VBComp.CodeModule
    ModuleSynchronised = True
End Function

Public Sub testModuleSynchronised()
    Dim r As Boolean
    r = ModuleSynchronised("NoSuchModule")
    r = ModuleSynchronised("ModuleImportControl")
End Sub

It currently dies on the line

 Set CodeMod = VBComp.CodeModule

since CodeMod is defined as VBIDE.CodeModule. I have a reference to "Microsoft Visual Basic 6.0 Extensibility"


It's something simple I know, I just can't think what it is!


--- Alistair.