网页登录无法正常使用excel VBA

时间:2015-12-23 13:07:04

标签: html excel-vba internet-explorer vba excel

我正在开发项目以从网站下载转储并将其保存在使用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>

2 个答案:

答案 0 :(得分: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. 将其合并到您的代码中,如下所示。
  3. 代码:

    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
    

答案 1 :(得分: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