2015-12-23 38 views
1

我正在从项目中下载转储从网站,并保存在使用Excel vba指定的路径。网页登录不起作用使用excel VBA

当您执行调试或按“F8”逐行执行时,代码正常工作。

但是当你通过按“F5”或者在分配宏之后点击按钮来执行整个程序。它不工作。

需要您宝贵的建议来解决此问题。

由于事先 人员Prasanna

VBA代码用于登录。网页的登录

Sub Login() 

Dim MyHTML_Element As IHTMLElement 
Dim MyURL As String 
Dim HTMLDoc As HTMLDocument 
Dim MyBrowser As InternetExplorer 


MyURL = "URL" 
Set MyBrowser = New InternetExplorer 
MyBrowser.Silent = True 
MyBrowser.Navigate MyURL 
MyBrowser.Visible = True 

Do 
Application.Wait DateAdd("s", 5, Now) 
Loop Until MyBrowser.READYSTATE = READYSTATE_COMPLETE 
Application.Wait DateAdd("s", 5, Now) 
Set HTMLDoc = MyBrowser.document 
    HTMLDoc.all.Country_Code.Value = "Country_Code" 
    HTMLDoc.all.Login.Value = "UserName" 
    HTMLDoc.all.passwd.Value = "Password" 
    HTMLDoc.all.Item("B1").Click 

     For Each MyHTML_Element In HTMLDoc.getElementsByName("B1") 
      If MyHTML_Element.Type = "button" Then MyHTML_Element.Click: Exit For 
     Next 

End sub 

的HTML代码示例。

   <table border=0> 

        <tr> 
         <td>Country:</td> 
         <td> 
          <input type="text" name="country_code" maxlength=2 
           onblur="this.value=this.value.toUpperCase();Form1_action(this.value)"> 
         </td> 
        </tr> 

        <tr> 
         <td>Language:</td> 
         <td> 
          <select name="idioma" disabled > 
           <option value="uk|es" onblur="document.Form1.login.focus()">ENGLISH</option> 
<option value="sp|es" onblur="document.Form1.login.focus()">SPANISH</option> 
<option value="fr|en-us" onblur="document.Form1.login.focus()">FRENCH</option> 
<option value="it|en-us" onblur="document.Form1.login.focus()">ITALIAN</option> 
<option value="de|de" onblur="document.Form1.login.focus()">GERMAN</option> 

          </select> 
         </td> 
        </tr> 

        <tr> 
         <td>Login:</td> 
         <td> 
          <input type="text" name="login" maxlength=10 value="" disabled > 
         </td> 
        </tr> 

        <tr> 
         <td>Password:</td> 
         <td> 
          <input type="password" autocomplete="off" name="passwd" maxlength=10 value="" disabled onkeypress="var okp=(event.which)?event.which:event.keyCode; if(okp==13) SiteRedirect(this.form)"> 
         </td> 
        </tr> 

       </table> 

       <br> 

       <center> 
        <input type="button" name="B1" value="Sign In" 
         onclick="SiteRedirect()" 
         disabled 
         style="width:80pt" 
         > 

       </center> 
+1

改变你做的循环,你让代码等待IE加载到'循环,当MyBrowser.ReadyState <> 4或MyBrowser.Busy' –

+0

嗨斯科特感谢你的回复...我试过上面的代码,但仍然没有工作......我猜测是VB代码执行速度比IE网页加载时间更快......我是否需要增加等待函数的时间跨度...... ???它会帮助...? – Prasanna

+0

是的,代码执行在VBA中是*独立*的浏览器加载时间,所以你必须对此作出说明,就像你已经是这样了。增加循环内的等待时间无关紧要,因为在IE未准备就绪时它将保持循环。我在回答中发布了我用于此的代码。在我经常使用的应用程序中,它适用于我。 –

回答

0

这是我用来允许IE加载一个经常与IE中的网页工作的应用程序的方法。在经历了很多反复试验之后,我已经发现了这一点,现在它一直在运行 - 尽管我已经看到了很多方法来实现这一点。

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 

Sub IEWait(IE As Object) 
'assumes IE is loaded as InternetExplorer.Application") 

With IE 
    Do While .Busy Or .ReadyState <> 4: Sleep (100): Loop 
End With 

End Sub 

您可以通过

  1. 配售公共您的模块窗口的顶部声明中定义的任何Sub之前添加到您的代码。
  2. 将它合并到您的代码中,如下所示。

代码:

With MyBrowser 
    .Silent = True 
    .Navigate MyURL 
    .Visible = True 

    Do While .Busy or .Readystate <> 4: Sleep (100): Loop 

    Set HTMLDoc = .document 

    '... rest of code 

End With 
+0

让我在本周结束时尝试使用你的代码......并且我会在星期天让你知道结果....再次感谢你scoot ...快乐的周末...并且圣诞快乐.... ... – Prasanna

+0

圣诞快乐给你@Prasanna!周末愉快,可能你心中的喜悦传遍你和你的生活,这个假期和更多:) –

0

斯科特击中了要害。

Sub Test() 

Dim IE As Object 
Set IE = CreateObject("InternetExplorer.Application") 
With IE 
    .Visible = True 
    .Navigate "http://www.marketwatch.com/investing/stock/aapl/analystestimates" ' should work for any URL 
    Do Until .ReadyState = 4: DoEvents: Loop 

. . . YOUR CODE HERE . . . 

End With 
End Sub 

OR

Sub DumpData() 

Set IE = CreateObject("InternetExplorer.Application") 
IE.Visible = True 

URL = "http://finance.yahoo.com/q?s=sbux&ql=1" 

'Wait for site to fully load 
IE.Navigate2 URL 
Do While IE.Busy = True 
    DoEvents 
Loop 

    . . . YOUR CODE HERE . . . 

End Sub