Results 1 to 4 of 4

Thread: [RESOLVED] CallByName and Late-Binding

  1. #1

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    Resolved [RESOLVED] CallByName and Late-Binding

    Wow, apparently, in the VBA, you can't use CallByName with late-bound object, unless I'm just blind and overlooking something.

    Here's some code I put into a UserForm1:

    Code:
    
    Option Explicit
    
    Public Sub Testing()
        MsgBox "got to testing"
    End Sub
    
    And here's some code I just threw into a Module1:

    Code:
    
    Option Explicit
    
    Private Sub EarlyBoundWithCallByName()
        CallByName UserForm1, "Testing", VbMethod
    End Sub
    
    Private Sub LateBoundWithCallByName()
        Dim frm As UserForm
        '
        Set frm = UserForm1
        CallByName frm, "Testing", VbMethod
    End Sub
    
    If I execute EarlyBoundWithCallByName, everything works fine. However, if I execute LateBoundWithCallByName, I get the following error.

    Name:  VbaErr.png
Views: 332
Size:  4.4 KB

    Hmmm, that's pretty sad.

    Y'all Take Care,
    Elroy

    EDIT1: Just for grins, I made sure the form was loaded and showing first, but it didn't make any difference. Same results. Here's how I modified my Module1 test code:

    Code:
    
    Option Explicit
    
    Private Sub EarlyBoundWithCallByName()
        UserForm1.Show 0
        CallByName UserForm1, "Testing", VbMethod
    End Sub
    
    Private Sub LateBoundWithCallByName()
        Dim frm As UserForm
        '
        UserForm1.Show 0
        Set frm = UserForm1
        CallByName frm, "Testing", VbMethod
    End Sub
    
    
    Last edited by Elroy; Apr 21st, 2018 at 02:24 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: CallByName and Late-Binding

    Errr.....?
    Shot in the dark
    The Latebound-Function:
    You're declaring frm inside the function (scope), but you're not creating it (no Set frm=New UserForm1), and you're loading USerForm1, not frm.

    But, as i said: A shot in the dark

    EDIT: Just saw your EDIT...... :-(

    EDIT2: Found something:
    https://stackoverflow.com/questions/...-ihtml-element

    You could try:
    Code:
    Dim frm As IUnknown
    Set frm = UserForm1
    
    CallByName frm, "Testing", VbMethod
    Last edited by Zvoni; Apr 21st, 2018 at 03:10 PM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    Re: CallByName and Late-Binding

    Hi Zvoni,

    Thanks for taking a look.

    However, I was thinking that the VBA has all those form names set as VB_PredeclaredId = True, just as VB6 does. I just exported one and took a look, and they do:

    Code:
    VERSION 5.00
    Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} UserForm1 
       Caption         =   "UserForm1"
       ClientHeight    =   3165
       ClientLeft      =   45
       ClientTop       =   390
       ClientWidth     =   4710
       OleObjectBlob   =   "UserForm1.frx":0000
       StartUpPosition =   1  'CenterOwner
    End
    Attribute VB_Name = "UserForm1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit
    Basically, this means that their name doubles as a predeclared global object variable that's declared with the New keyword. In other words, the moment you touch them, they'll auto-instantiate.

    Ahhhh, but using IUnknown actually worked. How about that!!
    I would have never guessed that using an even more late-bound method would have been the answer.

    Many Thanks,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4
    New Member Leith Ross's Avatar
    Join Date
    Feb 2018
    Posts
    9

    Re: CallByName and Late-Binding

    Hello Elroy,

    This worked for me. I set the ShowModal property to False before running the code.

    Code:
    Sub TestA()
    
            UserForm1.Show
            CallByName UserForm1, "Test", VbMethod
        
    End Sub
    
    Sub TestB()
    
        Dim frm As Object
        
            Set frm = UserForm1
            frm.Show
        
            CallByName frm, "Test", VbMethod
        
    End Sub

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