Kyle smith
Kyle smith

Reputation: 1

VBA website automation

Good Afternoon All,

I have created some code that automatically logs me into my works website, however when trying to get the VBA to enter value into one of the search boxes I always recieve error 438: Object doesn't support this propery or method.

This is my code below please can you help:

Option Explicit

Const MyUserName As String = "username"
Const MyPassword As String = "password"

Const READYSTATE_COMPLETE As Integer = 4

Dim objIE As Object


Sub LoginScript()

Dim Message, Title, Default
Dim MyValue As String
Dim currenttime As Date

Set objIE = CreateObject("InternetExplorer.Application")

    If MsgBox("Do you want to search for a member?", vbYesNo, "Login") = 
vbNo Then
With objIE
.Visible = True
.Silent = True
.Navigate ("https://XXXXXXXXX.com/")
Do Until .ReadyState = READYSTATE_COMPLETE
  DoEvents
Loop
.Document.all.txtuserid.Value = MyUserName
.Document.all.txtpassword.Value = MyPassword
.Document.all.save_button.Click
Do Until .ReadyState = READYSTATE_COMPLETE
  DoEvents
Loop
End With
    Else

    Message = "Please enter clients CTC number"    ' Set prompt.
    Title = "Member Search"    ' Set title.
    ' Display message, title, and default value.
     MyValue = InputBox(Message, Title)

With objIE
    .Visible = True
    .Silent = True
    .Navigate ("https://XXXXXXXXX.com/")
    Do Until .ReadyState = READYSTATE_COMPLETE
    DoEvents
    Loop
    .Document.all.txtuserid.Value = MyUserName
    .Document.all.txtpassword.Value = MyPassword
    .Document.all.save_button.Click
    Do Until .ReadyState = READYSTATE_COMPLETE
    DoEvents
    Loop
    currenttime = Now
    Do Until currenttime + TimeValue("00:00:10") <= Now
    Loop
    Do Until .ReadyState = READYSTATE_COMPLETE
    DoEvents
    Loop
    .Document.getElementsById("txtClientRef").Value = MyValue <------ Errors Here
End With
End If
End Sub

Upvotes: 0

Views: 386

Answers (1)

QHarr
QHarr

Reputation: 84465

Id should be unique, and the syntax is getElementById, so try:

 .Document.getElementById("txtClientRef").Value

It is also a good idea to do .Document.getElementById("txtClientRef").Focus first.

If this then yields runtime error 424 it may be either that the object doesn't exist, as specified, or is not available at the time you attempt the assignment. For the latter to you can attempt to loop until not nothing with a specified timeout.

Upvotes: 2

Related Questions