0

I'm a complete newbie for Excel VBA, but what I'm trying to accomplish is enter a text into a text box, whose code is

 <input name="txt_invoiceno" type="text" maxlength="16" id="txt_invoiceno" tabindex="1" class="text-left" style="width:150px;"> 

This is my VBA code

Dim Inv as Object
Inv = ie.Documents.getelementsbyId("txt_invoiceno")
Inv.value="mytext"

This just gives me an error

Object doesn't support this property or method.

Am I doing anything wrong? Please someone give me code to enter text into the element.

Mesut Akcan
  • 899
  • 7
  • 19
John Zavax
  • 1
  • 1
  • 3

2 Answers2

1

You Dim Inv As Object and objects need to use Set:

Set Inv = ie.Documents.getElementsById("txt_invoiceno")

Then you need to check if Inv was found, eg If Not Inv Is Nothing Then before you continue using it. Also make sure the id txt_invoiceno only exists once in the HTML document otherwise the HTML is not valid (and Inv is an array of textboxes).

For further information also see: How do I get the value of text input field using JavaScript? It is for JavaScript but works accordingly in VBA.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
0

First, as PEH said, after using Dim Inv As Object statement define the object, we need to use Set statement to set value.

Besides, please refer to the following options, if we want to find an element by ID property, it will return one element, and if we want to find elements by tag, class or name property, it will return multiple elements.

IE.document.getelementbyid("ID").value = "value"       'Find by ID
IE.document.getelementsbytagname("ID").value = "value"        'Find by tag
IE.document.getelementsbyclassname("ID").value = "value"      'Find by class
IE.document.getelementsbyname("ID").value = "value"           'Find by name

So, please modify your code as below (add Set statement, change documents to document, change getelementsbyId to getelementbyId):

    Dim Inv As Object
    Set Inv = IE.document.getelementbyId("txt_invoiceno")
    Inv.Value = "mytext"

Edit

The web page resource:

<form id="form1"> 
    <input name="txt_invoiceno" type="text" maxlength="16" id="txt_invoiceno" tabindex="1" class="text-left" style="width:150px;"> 
</form>

The VBA script:

Sub Test()
    Dim IE As Object

    Dim startDateText As Object, endDateText As Object

    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .Navigate "https://dillion132.github.io/default.html"

        While IE.ReadyState <> 4
            DoEvents
        Wend

        Dim Inv As Object
        Set Inv = IE.document.getelementbyId("txt_invoiceno")
        Inv.Value = "mytext"

    End With
    Set IE = Nothing
End Sub
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • Thank you for the response. But after replacing my old code with below code ```Dim Inv As Object Set Inv = ie.Document.getElementById("txt_invoiceno") Inv.Value = "190" ``` I get Object required error – John Zavax Jan 10 '20 at 18:36
  • Just found out that, the input box resides inside a form **form1** . Should we reference this, or not?? – John Zavax Jan 10 '20 at 21:56
  • @John, please refer to my edit and check the sample code, if using form tag, we could directly use `IE.document.getelementbyId` method to find the element inside the form. But, if you are using the **Iframe tag**, we should find the IFrame element first, then find an element from the IFrame's document. You could check these links: [link 1](https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba) and [link 2](https://social.msdn.microsoft.com/Forums/Lync/en-US/c8a0c910-a0de-412a-b5cc-cf9f930742e0/how-to-fetch-iframe-data-using-access-vba?forum=accessdev). – Zhi Lv Jan 15 '20 at 07:34