Need help with java script

Lakshmi Polisetti 20 Reputation points
2026-07-28T12:23:50.98+00:00
Private Sub WaitForBrowser(ByVal IE As Object, _
                           Optional ByVal timeoutSeconds As Long = 30)
    Dim startTime As Double
    Dim pageReady As Boolean
    startTime = Timer
    Do
        DoEvents
        pageReady = False
        On Error Resume Next
        If IE.Busy = False Then
            If IE.ReadyState = 4 Then
                If Not IE.Document Is Nothing Then
                    pageReady = True
                End If
            End If
        End If
        On Error GoTo 0
        If pageReady Then Exit Sub
        'Handle Timer resetting after midnight.
        If Timer < startTime Then startTime = Timer
        If Timer - startTime >= timeoutSeconds Then
            Err.Raise vbObjectError + 1001, _
                      "WaitForBrowser", _
                      "The webpage did not finish loading within " & _
                      timeoutSeconds & " seconds."
        End If
    Loop
End Sub
Option Explicit
Public Sub SearchWCISID()
    Dim IE As Object
    Dim htmlDoc As Object
    Dim wcisField As Object
    Dim searchButton As Object
    Dim websiteURL As String
    Dim wcisID As String
    On Error GoTo ErrorHandler
    'Replace this with your actual website address.
    websiteURL = "https://your-website-address"
    'WCIS ID will be taken from A1 of the active worksheet.
    wcisID = Trim$(CStr(ActiveSheet.Range("A1").Value))
    If Len(wcisID) = 0 Then
        MsgBox "Cell A1 is blank. Enter the WCIS ID in A1.", _
               vbExclamation, "WCIS Search"
        Exit Sub
    End If
    'Open the browser through the Internet Explorer COM object.
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate websiteURL
    'Wait for the initial webpage to load.
    WaitForBrowser IE, 30
    Set htmlDoc = IE.Document
    'Find the WCIS ID field.
    Set wcisField = htmlDoc.getElementById("customerClientID")
    If wcisField Is Nothing Then
        MsgBox "The WCIS ID field was not found." & vbCrLf & _
               "Expected element ID: customerClientID", _
               vbExclamation, "Element Not Found"
        Exit Sub
    End If
    'Populate the field.
    wcisField.Focus
    wcisField.Value = wcisID
    'Setting .Value through VBA may not automatically trigger onchange.
    'The website uses onchange=""enableSearch();"".
    On Error Resume Next
    wcisField.FireEvent "onchange"
    If Err.Number <> 0 Then
        Err.Clear
        htmlDoc.parentWindow.execScript "enableSearch();", "JavaScript"
    End If
    On Error GoTo ErrorHandler
    DoEvents
    Application.Wait Now + TimeValue("00:00:01")
    'Find the Search button.
    Set searchButton = htmlDoc.getElementById("searchBtn")
    If searchButton Is Nothing Then
        MsgBox "The Search button was not found." & vbCrLf & _
               "Expected element ID: searchBtn", _
               vbExclamation, "Element Not Found"
        Exit Sub
    End If
    'Run enableSearch once more if the button remains disabled.
    If searchButton.disabled Then
        On Error Resume Next
        htmlDoc.parentWindow.execScript "enableSearch();", "JavaScript"
        On Error GoTo ErrorHandler
        DoEvents
        Application.Wait Now + TimeValue("00:00:01")
    End If
    'Click Search.
    searchButton.Click
    'Wait for the search results page.
    WaitForBrowser IE, 30
    MsgBox "WCIS ID submitted successfully:" & vbCrLf & _
           wcisID, vbInformation, "WCIS Search"
    'Do not close the browser.
    'Keep the IE object available while testing.
    Exit Sub
ErrorHandler:
    MsgBox "The automation could not be completed." & vbCrLf & vbCrLf & _
           "Error " & Err.Number & ": " & Err.Description, _
           vbCritical, "WCIS Automation"
End Sub

Private Sub WaitForBrowser(ByVal IE As Object, _
Microsoft 365 and Office | Development | Office JavaScript API
0 comments No comments

1 answer

Sort by: Most helpful
  1. Marcin Policht 99,145 Reputation points MVP Volunteer Moderator
    2026-07-28T14:47:06.97+00:00

    looks like this is a self-running JavaScript automation script. The (async () => { ... })(); wrapper executes the code and allows it to use await for delays and waiting on page updates.

    The script automates processing "hit" records in a web application. For each hit, it clicks Investigate, selects "Need More Information" from the decision dropdown, clicks Save, then moves to the next hit. When all hits in the current batch are complete, it opens the next batch and continues.

    The CONFIG object stores settings such as the dropdown value, delays, timeouts, and a safety limit of 5,000 hits. Helper functions locate page elements, verify they are visible and enabled, and wait for them to become available before interacting with them.

    The main loop repeats until all hits are processed, you stop it by running window.stopHitAutomation = true, an error occurs, or the safety limit is reached. Throughout execution, the script logs progress, validates key actions, and displays an alert when it completes or stops due to an error.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.