3

Im a fresher to Excel Macros (VBA) and looking for a way to automate IE to login to a website and proceed to a certain page. website URL : https://www.mast-technicalservices.com/ecp/index2.jsp and need to fill the login details and click continue.

Sub WebsiteLogIn()

Dim nURL As String
Dim UNElementID As String
Dim UserName As String
Dim PWElementID As String
Dim Password As String
Dim SIElementID As String


Set LoginData = ThisWorkbook.Sheets("Sheet1")
Set nURL = "https://www.mast-technicalservices.com/ecp/index2.jsp"
Set UNElementID = "ecp_param_userId"
Set UserName = LoginData.Cells(1, "B").Value
Set PWElementID = "ecp_param_password"
Set Password = LoginData.Cells(2, "B").Value
Set SIElementID = "imageField2"

Dim IE              As Object
Dim IEPage          As Object
Dim IEPageElement   As Object


    'Create a new Internet Explorer instance, make it visible and maximize its window and navigate to url.
    On Error Resume Next
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    ShowWindow IE.hwnd, SW_MAXIMIZE

    IE.navigate URL

    Set IEPage = IE.document

    'setthe UserName text box using the element ID.
    Set IEPageElement = IEPage.getElementById(UNElementID)

    'set the Password text box using the element ID.
    Set IEPageElement = IEPage.getElementById(PWElementID)


    'set the Continue button using the element ID.
    Set IEPageElement = IEPage.getElementById(SIElementID)


End Sub
SEEDS Support
  • 33
  • 1
  • 1
  • 6
  • Are you using a `WebBrowser` ActiveX control in a User Form, or an `XMLHttpRequest` object in a User Module? – Dai Feb 22 '18 at 04:12
  • Please post the code you have tried so people can assist you with errors/issues. You are welcome to search for similar questions to find the answer you are looking for. To try to help you along, I suggest you look at Powershell and UI-Automation (https://archive.codeplex.com/?p=uiautomation). I have used that combination to auto launch Internet Explorer and login a saved username and password along with additional steps. If you want to move forward with powershelgl, update your question and I'll try to assist. – SteveB Feb 22 '18 at 04:14
  • i haven't started anything to on this, basically im looking for help to get this done. – SEEDS Support Feb 22 '18 at 04:14
  • I have tried this to navigate to URL but has some issues. – SEEDS Support Feb 22 '18 at 04:23

1 Answers1

4

This works:

Sub login()

    Const Url$ = "https://www.mast-technicalservices.com/ecp/index2.jsp"

    Dim UserName As String, Password As String, LoginData As Worksheet
    Set LoginData = ThisWorkbook.Worksheets("Sheet1")
    UserName = LoginData.Cells(1, "B").Value
    Password = LoginData.Cells(2, "B").Value

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

    With ie

        .navigate Url
        ieBusy ie
        .Visible = True

        Dim oLogin As Object, oPassword As Object
        Set oLogin = .document.getElementsByName("ecp_param_userId")(0)
        Set oPassword = .document.getElementsByName("ecp_param_password")(0)

        oLogin.Value = UserName
        oPassword.Value = Password
        .document.forms(0).submit

    End With

End Sub

Sub ieBusy(ie As Object)
    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop
End Sub

The other sub: ieBusy will ensure the webpage is fully loaded prior to you attempting to manipulate it.

Community
  • 1
  • 1
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • I tried the code but i get a run-time error "Automation error and Unspecified error " Debug points to line Do While ie.Busy Or ie.readyState < READYSTATE_COMPLETE – SEEDS Support Feb 22 '18 at 05:43
  • Any more error details? Did it point to a specific line? I tried the code myself and it worked (I admit however, it was prior to my most current update). – K.Dᴀᴠɪs Feb 22 '18 at 05:44
  • i have tried to use READYSTATE option few times earlier and those failed as well. Is there any other controls or references that i have to enable to use this code? – SEEDS Support Feb 22 '18 at 06:04
  • ooops that's because you are using latebinding. Replace READYSTATE_COMPLETE with the number `4`. – K.Dᴀᴠɪs Feb 22 '18 at 06:05
  • Still the same. debug points to this line Do While ie.Busy Or ie.readyState < 4 – SEEDS Support Feb 22 '18 at 07:02
  • @SEEDSSupport once again, case of the "Late Binding". I apologize! Replace `(ie As InternetExplorer)` with `(ie As Object)` – K.Dᴀᴠɪs Feb 22 '18 at 07:04
  • Also, make sure in IE settings that you have Enable Protected Mode checked under Security. – QHarr Feb 22 '18 at 07:26
  • worked like a charm, now we have o go to the next step. I need to click one of the buttons in the next page and then select from a menu on the following page. problem is i cannot give out the login information.how can we do it.is it OK if i give you the source of the site. – SEEDS Support Feb 22 '18 at 08:25
  • @SEEDSSupport I am not sure what to say. Did you completely delete your code and replace with mine? Do you have any publicly declared variables? – K.Dᴀᴠɪs Feb 22 '18 at 08:30
  • @Davis , I have used your code to login to the page.now the next challenge is to select a button from the next page which will go to another page where i need to select on item from a menu.is there a secure way to share a login detail to you. – SEEDS Support Feb 22 '18 at 08:41
  • @SEEDSSupport Although I consider myself 100% trustworthy, I would hate for leakage somehow. Could you possibly take a screen shot of the html code in question? Or type it out in a question? – K.Dᴀᴠɪs Feb 22 '18 at 08:48
  • Here is the code for the element that i told about. – SEEDS Support Feb 22 '18 at 08:58