VB CallByName - Error message:Object required

Posted on 2008-11-11
Last Modified: 2012-05-05
Hi All,
I'm working on a little routine that will use the CallbyName function to call a function on a class that I wrote. This is all in Excel VBA. Now, I get an error on the line
" CallByName TextBoxObject, "SetTextBox", CallType.VbSet, Arguments()"
Error : Object required.
I'm doing something wrong (obviously!!) but I still don't think i've fully got my head around "CallByName"
What i'm essentially trying to achieve is to replace the line:
TextBox.SetTextBox "textboxa", "Hello A" by calling the CallByName function

Can anyone give some guidence - please!!!

Sub Test2()

    Dim TextBox As ClassTextBox

    Dim Arguments(1) As String

    Dim TextBoxCollection As Object

    Dim TextBoxObject As Object


    Set mIE = New SHDocVw.InternetExplorer

    mIE.Visible = True

    ' Set the URL

    mIE.Navigate "C:\IE Automation Test Page.html"


    ' TextBox.SetTextBox "textboxa", "Hello A"

    Arguments(0) = "textboxa"

    Arguments(1) = "HELLO A"

    Set TextBoxCollection = mIE.Document.getElementsByName("textboxa")

    Set TextBoxObject = TextBoxCollection(0)

    CallByName TextBoxObject, "SetTextBox", CallType.VbSet, Arguments()

End Sub

ClassTextBox: . . . 

Option Explicit

Public Function SetTextBox(TextBoxName As String, TextBoxValue As String)


    Dim TextBox As Object


    Set TextBox = mIE.Document.getElementsByName(TextBoxName)

    TextBox(0).Value = TextBoxValue


End Function

Open in new window

Question by:paddykool
    LVL 15

    Expert Comment

    I believe to replace the line TextBox.SetTextBox "textboxa", "Hello A"

    I'm guessing that you are setting the text not the textbox.   And the value you want to pass is the text for it.

    for that you should need

      CallByName TextBoxObject, "SetText", CallType.VbSet, "HELLO A"


    Author Comment

    Thanks for the reply. I'm still getting the error "Object Required".
    I think its something to do with the way my method is made in my ClassTextBox.
    If you look at my SetTextBox method (which yes, sets text inside a text box), it take two arguments, the name attribute of the textbox and the value to set inside it. This is why I was putting both of these arguments into the arguments() array.
    I done a watch on my TextBoxObject and it fine. Thought it to be a buit dogey as getElementsByName returns a collection.
    So . . . where am I going wrong?!?
    Also, can you explain the useCallType?!? aside, would it be possible to use CallType.Method as setTextBox is a method?!?
    All help greatly appreciated

    Author Comment

    Also, perhaps its something to do with the fact that its a DOM object?!?
    LVL 85

    Accepted Solution

    Try this way:

        CallByName TextBoxObject, "SetTextBox", VbMethod, "textboxa", "HELLO A"
    LVL 15

    Assisted Solution

    I think that Set TextBoxCollection = mIE.Document.getElementsByName("textboxa") is returning a standard web text box.  It may not know about your specific obects.

    At that point you have a reference to the one object that you want to set the text into anyways.   You should be able to just setText.

    using method instead of set is probably correct if you do not have both a set and get implemented with that name.   They are probably interpreted differently somehow.

    Author Comment

    Thanks but unfortunately
    " CallByName TextBoxObject, "SetTextBox", VbMethod, "textboxa", "HELLO A" did not work
    However, it did produce an error message "Object does not support this property or method...hmmm
    This is coming from using "VbMethod" rather than "CallType.VbSet" (or CallType.vbMethod)
    Anywho, there is something unorganised about the way i'm doing this.
    Whats the deal with using "SetText" insterad of "SetTextBox"?!? I have to use my method "SetTextBox" as i want to use lots of other functions in ClassTextBox by this way.

    To give a breif outline of what i'm up to  (stop reading from here as this may bore...!!!)
    I have a spread sheet with 3 columns
    Control, Method, Data
    All the columns contain key word that will drive the script
    Control contains the class (ClassTextBox), and the HTML name of the textbox (textboxa)
    Method contains the method in my ClassTextBox (SetTextBox)
    Data contain the data to use with the methed call (HELLO A)
    What I'm trying to do here is create a driver script that will read of this sheet, do a spot of parsing and pass the nessary arguments to the function CallByName to perform the task on web page...

    Now, I'm going to stick with the above for now but aside, anyone got a better suggestion for what I'm try to?!? I'll open a different tread perhaps but sure we'll stick with the issue at hand for now.
    Thanks people!!!

    Author Comment

    Oh, SRigney: yes, Set TextBoxCollection = mIE.Document.getElementsByName("textboxa") is returning a collect but I'm working with a HTML page that i made and it has only one textbox named textboxa in it.
    Also, the following line Set TextBoxObject = TextBoxCollection(0) creates a reference to that specific object.
    Thanks :)

    Author Comment

    Hi All,
    I found the solution!!!

    By adding the lines:
    Dim c As ClassTextBox
    Set c = New ClassTextBox

    and then calling the function using

    CallByName c, "SetTextBox", VbMethod, "textboxa", "HELLO A"

    The correct action was performed. I got confused as I was passing in the actual object instead of and instance of my class

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now