0

I've looked through several forums, but unfortunately my html skills are not what they should be. Can somebody please help me find the correct command to log onto www.emo.no with Excel VBA? (See code in bottom).

I have successfully managed to fill in the google searchbar with this code:

Sub Google_Test()

Set ie = CreateObject("InternetExplorer.application")

Bnavn = "UserNa"

With ie
    .Visible = True
    .navigate "www.google.com"

Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop

ie.document.all("q").Value = Bnavn


Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop


End With
Set ie = Nothing

End Sub

I am trying to have Excel VBA log into the site www.emo.no for me, but am unable to find the correct fields to fill in.

The code on the webpage i am trying to have Excel VBA log onto is:

    <div id="PcLogin">

        <script language="Javascript">
        var locked = false;

      function doLogin() {
        document.login.uid.value = document.login.uid.value.toLowerCase();
        document.login.pwd.value = document.login.pwd.value.toLowerCase();
        return true;
        }


if(!document.all){
  document.captureEvents(Event.CLICK | Event.FOCUS);

}

/* Code for putting the visible word "Username" in the userID field */

 function changeBoxUid()
 {
    document.getElementById('uid1').style.display='none';
    document.getElementById('uid2').style.display='';
    document.getElementById('uid').focus();
 }

 function restoreBoxUid()
 {
    if(document.getElementById('uid').value=='')
    {
      document.getElementById('uid1').style.display='';
      document.getElementById('uid2').style.display='none';
    }
 }
/* End code for userID field */


/* Code for putting the visible word "Password" in the password field */

 function changeBoxPwd()
 {
    document.getElementById('div1').style.display='none';
    document.getElementById('div2').style.display='';
    document.getElementById('pwd').focus();
 }
 function restoreBoxPwd()
 {
    if(document.getElementById('pwd').value=='')
    {
      document.getElementById('div1').style.display='';
      document.getElementById('div2').style.display='none';

    }
 }
/* End code for password field */

</script><div id="loginBody">
<form target="_top" method="post" id="login" name="login" action="ePortal/ctrl" onsubmit="
                if (locked) {
                    return false; 
                } 
                else 
                {
                    locked=true;
                    doLogin();
                };          
            ">
<div class="PcLoginPositionInput">
<input value="user" name="action" type="hidden"><input value="login" name="spec" type="hidden">
<div id="uid1">
<input onfocus="changeBoxUid()" name="uid_temp" type="text" class="PcLoginInputFields" value="Brukernavn">
</div>
<div id="uid2" style="display: none;">
<input onblur="restoreBoxUid()" value="" id="uid" name="uid" type="text" class="PcLoginInputFields">
</div>
<div id="div1">
<input onfocus="changeBoxPwd()" type="text" name="pass_temp" class="PcLoginInputFields" value="Passord">
</div>
<div style="display:none" id="div2">
<input onblur="restoreBoxPwd()" value="" type="password" id="pwd" name="pwd" class="PcLoginInputFields">
</div>
</div>
<div class="PcLoginForgottenPwd">
<a class="login" href="ePortal/ctrl?action=requestnewpassword">Glemt passord?</a>
</div>
<div id="PcLoginRegister"></div>
<input value="" type="submit" class="login_rollover">
</form>
</div>


    </div>

I've tried "uid", "uid1", "uid2", "uid_temp", "div1", "div2", "pass_temp" and "pwd", but there is no action on screen.

Here is the code i am using for the site:

Sub EMO_login()
' Source for this code:
' http://stackoverflow.com/questions/24038230/fill-user-name-and-password-in-a-webpage-using-vba
' http://stackoverflow.com/questions/25319983/logging-in-to-website-with-excel-vba

Set ie = CreateObject("InternetExplorer.application")

Bnavn = User1
Pw = testPass1

With ie
    .Visible = True
    .navigate "www.emo.no"

Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop


'This is where the input command should be.


Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop


End With
Set ie = Nothing

End Sub

I am blank as to what to put in here. Can anybody show me the command to fill in the two fields as well as submit and log in?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Hermann
  • 27
  • 3
  • 9
  • I suggest looking into `getElementById` and `getElementsByTagName`. You could use `ie.document.getElementById("uid2").getElementsByTagName("input")(0).Value=...` – nicolaus-hee Jul 06 '15 at 05:20
  • I still can't get the form filled in. – Hermann Jul 06 '15 at 23:05
  • What error do you get and on which line? – nicolaus-hee Jul 07 '15 at 00:02
  • I'm not getting an error. The macro runs fine, but there is no visible data in the form. I tried to manualy submit it (after running the script), and there is definately a value there, so it seems to work, however i am struggeling to find the code to submit the form as well. Would you be kind enough to help me with that as well? – Hermann Jul 07 '15 at 05:05
  • Try `ie.document.forms(0).submit` – nicolaus-hee Jul 07 '15 at 07:48
  • @nhee That did not work, but altering the same logic and commands to ie.document.forms("login").submit worked like a dream. Thanks for all your help! :-) – Hermann Jul 07 '15 at 15:26
  • You're welcome. You should summarize your solution and post (and accept) it as an answer to this question. – nicolaus-hee Jul 07 '15 at 15:40

1 Answers1

0

Thans to @nhee for helping me find a solution.

The working code looks like this:

Sub EMO_login()

Dim ie As Object
Dim sht As Worksheet

Set sht = Sheet8
Set ie = CreateObject("InternetExplorer.application")

With ie
    .Visible = True
    .navigate "www.emo.no"

Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop

'Login procedure
ie.document.getElementById("uid2").getElementsByTagName("input")(0).Value = "MyUsername"
ie.document.getElementById("div2").getElementsByTagName("input")(0).Value = "MyPassword"
ie.document.forms("login").submit

Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop

End With
Set ie = Nothing

End Sub
Hermann
  • 27
  • 3
  • 9