Home » excel » excel – VBA- Can't get data passed to website form

excel – VBA- Can't get data passed to website form

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to pass data into a form that is on the fourth page. I have successfully navigated through the macro signing me in, searching for specific page based on data entered, going to that specific page, and pulling up the form in which I want to enter data into multiple spots.

Sub Test()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "http://mywebsite.com/Pages/MainSite/Default.aspx"
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
    IE.document.getElementById("txtUserName").Value = "dummyusername"
    IE.document.getElementById("txtPassword").Value = "password"
    IE.document.getElementById("btnLogin").Click
End Sub

Sub Next2()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "mywebsite.com/pages/hr/TimeEntryDashboard.aspx"
    Do While IE.Busy
        Application.Wait DateAdd("s", 2, Now)
    Loop
    IE.document.getElementById("MainContent_MainContent_txtStartDateFilter").Value = "10/13/2018"
    IE.document.getElementById("MainContent_MainContent_txtEmployeeNameFilter").Value = "122631"
    IE.document.getElementById("MainContent_MainContent_btnFind").Click
End Sub

Sub Next3()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "mywebsite.com/pages/hr/TimeAndInspectionWizard.aspx?id=0245b750-4cde-47da-a754-fb7f8bfecfc9"
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
    IE.document.getElementById("imgGridTimeDetailsArrow0").Click
    IE.document.getElementById("MainContent_MainContent_tcMain_tpValidation_rptInspectionDetails_imbGridInspectionDetailsOptionsAdd_0").Click
End Sub

Sub Next4()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
  ' IE.Visible = True
  ' IE.Navigate "mywebsite.com/pages/hr/TimeAndInspectionWizard.aspx?id=0245b750-4cde-47da-a754-fb7f8bfecfc9"
    Do While IE.Busy
        Application.Wait DateAdd("s", 2, Now)
    Loop
    IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionDetailsLotNumber").Value = "12345"
    IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionInspectedQuantity").Value = "1"
    IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtGoodWithoutReworkQuantity").Value = "1"
    IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtAddInspectionResult").Click
End Sub

This part above is producing an error at the first section where it is supposed to input data onto the screen.

How to&Answers:

You must check whether an element exists or not as part of code validation and that will also lead you to identifying the problem in better manner. Here is your code re-written with validation for example.

Sub Next4()
    Dim IE As Object, Elem As Object
    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        Do While .Busy
            Application.Wait DateAdd("s", 2, Now)
        Loop

        With .document

            Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionDetailsLotNumber")
            If Not Elem is nothing Then
                Elem.Value = "12345"
                Set Elem = Nothing
            End If

            Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionInspectedQuantity")
            If Not Elem is nothing Then
                Elem.Value = "1"
                Set Elem = Nothing
            End If

            Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtGoodWithoutReworkQuantity")
            If Not Elem is nothing Then
                Elem.Value = "1"
                Set Elem = Nothing
            End If

            Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtAddInspectionResult")
            If Not Elem is nothing Then
                Elem.Click
            Else
                MsgBox "Can't Click as Element Missing : " & "MainContent_MainContent_tcMain_tpInspectionDetails_txtAddInspectionResult"
            End If
       End With
    End With
End Sub

Answer:

You haven’t stated the error or shown any HTML. It could be that your element is within a parent frame/iframe.

  1. You do want a proper wait for page load While .Busy Or .readyState < 4: DoEvents: Wend. If you join your subs togther you will need this after each .Click/.Submit/.Navigate
  2. If it is a timing issue then you can have a loop timer, as shown, to loop until element set or timeout reached – to avoid infinite loop
  3. You do want a test for if element was set
  4. You do want the attempt to assign an object reference wrapped within an On Error Resume Next to keep the code running and prevent users from seeing error messages if element not set.
  5. Your question is about this first part so that I have addressed but I echo the sentiment in the comments that you only need one IE instance and should work with that unless these subs are unrelated and run independently. You may still consider whether they can be joined in that case using one instance.

VBA:

Option Explicit
Public Sub LoopUntilSet()
    Dim IE As New InternetExplorer, t As Date
    Const MAX_WAIT_SEC As Long = 5

    With IE
        .Visible = True
        .navigate "yourURL"

        While .Busy Or .readyState < 4: DoEvents: Wend

        t = Timer
        Do While x Is Nothing
            DoEvents
            On Error Resume Next
            Set ele = .document.getElementById("txtUserName")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop

        If ele Is Nothing Then
            Exit Sub
        Else
            ele.Value = "dummyusername"
            'other code
        End If
        .Quit
    End With
End Sub