Results 1 to 4 of 4

Thread: implements interfaces in VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    1

    implements interfaces in VBA

    Hi,
    I am using MS Excel and Im trying to write a macro(VBA) that would use classes and implement an interface. I have no idea what im doing wrong but i keeping getting an error prompting me that the Object module needs to implement a function that the interface has declared. But strangely the function is already implemented. My code is a follows:

    clsInterface
    VB Code:
    1. Public Property Get LibraryItem_CheckedOut() As Boolean
    2. End Property

    clsDerived
    VB Code:
    1. Implements clsInterface
    2.  
    3. Public Sub Class_Initialize()
    4.     MsgBox "in derived"
    5. End Sub
    6.  
    7. Public Property Get LibraryItem_CheckedOut() As Boolean
    8.     MsgBox "in derived"
    9. End Property

    And finally, ThisWorkbook
    VB Code:
    1. Dim cls As clsInterface
    2.     Dim derived As clsDerived
    3.    
    4.     Set cls = New clsInterface
    5.     Set derived = new clsDerived
    6.     Set cls = derived

    Ive wrecked my brains of this for hours and have not figured out why i get the error. Again, the error is: "Object module needs to implement 'LibraryItem_CheckedOut' for interface clsInterface

    Thanks a million
    raven

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: implements interfaces in VBA

    I might be wrong, but it looks like you're trying to use .Net code in VBA. I've never heard of Implements & Interfaces in VBA. .Net, Java, yes. Maybe the latest VBA supports that stuff?
    Tengo mas preguntas que contestas

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: implements interfaces in VBA

    Implements is VB6 & VBA and in .NET its there but in a different form, its in the Framework.

    Just like when you write an Add-In for Outlook or other Office app. It will use the Implements IDTExtensibility2 interface. This is the
    same thing excep raven000 has written a custom class to implement.


    Ps, Welcome to the Forums, Raven000.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    New Member
    Join Date
    May 2015
    Posts
    1

    Re: implements interfaces in VBA

    This question is a couple months away from being 10 years old but it's still unanswered and shows up in Google search results so I wanted to answer it. The properties/functions/subs defined in the interface need to be prefixed by the interface name and an underscore. Also, it can be initialized in an easier way than initially posted. Changes below.

    You can set up properties/functions/subs in the constructor(derived) class without prefixing them or putting them in the interface but they can't be called from outside the class (even if they are public) and are not visible in intellisense. When they are in the interface and the class (as public) they can be called from outside the class and are visible.

    clsInterface
    VB Code:
    1. Public Property Get LibraryItem_CheckedOut() As Boolean
    2. End Property

    clsDerived
    VB Code:
    1. Implements clsInterface
    2.  
    3. Public Sub Class_Initialize()
    4.     MsgBox "in derived"
    5. End Sub
    6.  
    7. Public Property Get clsInterface_LibraryItem_CheckedOut() As Boolean
    8.     MsgBox "in derived"
    9. End Property

    ThisWorkbook
    VB Code:
    1. Dim cls As clsInterface
    2.     Set cls = New clsDerived

    Sorry for bringing up such an old post but I've stumbled onto it multiple times in the last couple of years.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width