Request for a VBA code to login

Chaozfate

Board Regular
Joined
Mar 15, 2017
Messages
58
Hi Gents, i wish to access via VBA to a webpage which having the following HTML code.

Wonder if anyone could help with it. I search thru the forum as well as googled around, but keep getting error code 424/ error code 91.

Appreciate if anyone can help to draft the VBA code. Thanks alot.

HTML:
<form name="frmLogin" id="frmLogin" method="post" action="secu_login_select_bnch.asp" onsubmit="return Validate(this);">
  <table width="100%" border="0" cellspacing="2" cellpadding="0">
    <!-- Login Information -->
    <tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr bgcolor='#3A75C9'><td class='caption' align='absleft' width='1%'><img src='../images/spacer.gif' width='11'></td><td class='caption' align='absleft' width='25%' bgcolor='#3A75C9' nowrap><font color='#FFFFFF'>Login</font></td><td align='absleft' width='100%'>&nbsp;</td><td align='absleft' width='1%'><img src='../images/corner_tran.gif' width='25'></td></tr></table></td></tr>
    <tr><td>
      <table width="100%" border="0" cellspacing="1" cellpadding="4">
        <tr class="cell_bgcolor">
          <td class="required" valign="top" width="20%">Login ID</td>
          <td valign="top" width="80%">
            <input class="textbox" type="text" name="login_id" value="" maxlength="20" size="20">
          </td>
        </tr>
        <tr class="cell_bgcolor">
          <td class="required" valign="top" width="20%">Password</td>
          <td valign="top" width="80%">
            <input class="textbox" type="password" name="login_password" value="" maxlength="60" size="20">
          </td>
        </tr>
      </table>
    </td></tr>
    <tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td width='100%' height='1' bgcolor='#3A75C9'><img src='../images/spacer.gif' width='100%' height='1'></td></tr></table></td></tr>
    
    <tr><td>
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td align="left">
            <input class="ActionBtn" type="submit" name="btn_submit" id="btn_submit" value="Log In">
          </td>
          <td class="MessageWarning" align="right">
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,300
Office Version
  1. 365
Platform
  1. Windows
Hi there, could you add the VBA code you created so far? Thanks
 

Chaozfate

Board Regular
Joined
Mar 15, 2017
Messages
58
Hi, following is my VBA code, however allowed me to explain further.

- I am so sorry that I cant provide the actual link due to its an inter-company website. Understand alot of codes need to go thru the try and error process, but its very unfortunate that I cant provide the link to do so. If helps cant be provided without the actual website, it will be fine. Your approach is already very much appreciated.
- My vba code following comes with the "infoblock...." and "overridelink...." code, is to get thru the security certificate warning page. i did get thru with these code and arrive at the login page, but i have no idea why i cant get thru the login page with various of codes tried. i tried queryselector etc and keep getting the eror 404, error 91 etc.


VBA Code:
Sub BrowseToSite()
    Dim URL As String
    Dim ID As String
    Dim passw As String
          
    ID = "user"
    passw = "password"
    URL = website
    
    Set ie = CreateObject("internetexplorer.application")
    
    With ie

    .Visible = True
    .Navigate ("website")

    While ie.ReadyState <> 4
        DoEvents
    Wend

    ie.document.getElementById("infoBlockIDImage").Click
    ie.document.getElementById("overridelink").Click
     ie.document.getElementById("login_id").Value = ID
      
    
End With

End Sub
 

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,300
Office Version
  1. 365
Platform
  1. Windows
Hi there,
okay, let me try to help you with some general tips:
  • step through your code with F8, don't run it all at once with F5/start. In that way, you can check what your variables look like, inspect IE etc.
  • Split your command:
    ie.document.getElementBywebId("login_id").Value = ID
    To:
    Set LoginBox = ie.document.getElementById("login_id")
    LoginBox.Value = ID
    'Again: step through to see where it fails/crashes. Having it split into smaller chuncks combined with F8, the Local Variables window makes that you don't have a binary outcome (works vs not works) but can see which bit fails. It seems that login_id is a Name, not an Id, so it could be you need getElementsByName
  • If you're automating lots of data, it's easier to use MSXML2.XMLHTTP based web interaction instead of automating IE, see e.g. XmlHttpRequest - Http requests in Excel VBA - Coding is Love Having said that, if IE works, it works :).
Hope that helps a bit,
Koen
 

Chaozfate

Board Regular
Joined
Mar 15, 2017
Messages
58

ADVERTISEMENT

Thanks Koen,

In fact, i am just gotten into VBA 2 weeks ago, so what i did is just google + try & error. I tried the above suggested code but return with error 438

Now this XMLHTTPRequest is a new thing that i need to study again. I will try this.

Appreciate for your respond.
 

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,300
Office Version
  1. 365
Platform
  1. Windows
Hi Chaozfate,

I remember my first VBA project a long time ago, it felt great to get it to work after a lot of frustration (I remember that too...). So welcome to a great learning curve, VBA/programming is a great skill to have and/or impress collegues ;).
This is a good overview of the debug options in VBA: https://www.myonlinetraininghub.com/debugging-vba-code

On what line does your code crash? Is that the Set LoginBox = ? or only when you try to assign that value with LoginBox.Value = ?

The 438 error could be partially IE-related, I found this link: [SOLVED] Microsoft Run time Error 438 Windows Code Problem , suggesting it could maybe be solved by Disabling the Enhanced Protected Mode from Internet Explorer.

Cheers,
Koen
 

Chaozfate

Board Regular
Joined
Mar 15, 2017
Messages
58
Hi Rijnsent,

Yea, VBA is fun, the process of understanding and implementing the code is very satisfying. The try and error part is indeed frustrating but somehow enjoying. Especially with this forum community existence which ready to help whenever anyone need one. Can't deny on the impress colleagues part, HAHA!.

Back to my VBA code, the error 438 pop up without showing any line crash. I assume the code works by itself but somehow my company security system in check? I am in finance department so not really sure how does my company's security work. The enhanced protected mode was unchecked by default, and unfortunately i can't really try on the other method from the link you provided given that i have no access right to change any system related on my desktop.

I guess with those limitation, we cant really work anything out.

Thanks again for your kind response. Really appreciated.
 

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,300
Office Version
  1. 365
Platform
  1. Windows
" Back to my VBA code, the error 438 pop up without showing any line crash. "
-> If you step through your code by using F8 in the Visual Basic editor (not starting the code with the start arrow or F5), you'll see a line turning yellow and every time you press F8 the code will execute that line and step to the next line. Doing that will probably cause your code to crash/show that error when you try to execute a certain line. That is the one you're looking for. It might indeed be that your code won't show on which line something went wrong when you run it as a block/completely. When you find the culprit, try to split the command like I did in my first reply and see if that helps/narrows down the issue.
When your code gets bigger you would normally use F9/stops in your code to run the code to that point and take it with F8 from there. See the mini-manual below :)
Cheers,
Koen


VBA_debug3.png
 

Watch MrExcel Video

Forum statistics

Threads
1,133,459
Messages
5,658,893
Members
418,475
Latest member
ExcelBeginner233

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top