在VBA中使用InternetExplorer.Application

在VBA中使用InternetExplorer.Application,vba,internet-explorer,excel,Vba,Internet Explorer,Excel,我正在尝试构建一个简单的宏,它允许用户从excel工作表中选择所需的行并将其导出到web服务。宏应该能够使用用户名和密码对用户进行身份验证,以确保用户有权上载数据。当用户选择所需行时,IE将打开web服务的身份验证页面。当用户成功登录到web服务以验证凭据时,我当前正在尝试捕获url更改 是否有更好的方法对用户进行身份验证?我决定使用网站主登录页进行身份验证,因为我不想通过脚本发送用户名和密码 下面是我的代码的样子: Call NavigateToURL(--url of login page-

我正在尝试构建一个简单的宏,它允许用户从excel工作表中选择所需的行并将其导出到web服务。宏应该能够使用用户名和密码对用户进行身份验证,以确保用户有权上载数据。当用户选择所需行时,IE将打开web服务的身份验证页面。当用户成功登录到web服务以验证凭据时,我当前正在尝试捕获url更改

是否有更好的方法对用户进行身份验证?我决定使用网站主登录页进行身份验证,因为我不想通过脚本发送用户名和密码

下面是我的代码的样子:

Call NavigateToURL(--url of login page--) 

Public Sub NavigateToURL(ByVal argURL As String)

Dim objIE As Object

Set objIE = CreateObject("InternetExplorer.Application")

With objIE
.Visible = True
.Silent = True
.Navigate argURL

Do Until objIE.LocationURL() = (--url after successfull log in--)
 DoEvents

Loop

If n = (---url after successful log in--) Then
    objIE.Navigate (--redirect to another url)

Else
    MsgBox ("Sorry. Authentication Failed.")
End If

但是Do-Until循环之后的部分工作不正常。有人能指出我哪里出了错吗

我认为,一种更简单的方法是通过
输入框(或者自定义用户表单,如果你想让它更漂亮的话)获取用户名和密码。这就是想法:

Public Sub Main()
'get username and password through input boxes
username = InputBox("Type your username")
password = InputBox("Type your password")
NavigateToUrl(argURL, username, password)
End Sub   

Public Sub NavigateToURL(ByVal argURL As String, ByVal username As String, ByVal password As String)
    Dim objIE As Object
    Set objIE = CreateObject("InternetExplorer.Application")
    With objIE
        .Visible = True
        .Silent = True
        .Navigate argURL

    Do While .Busy
        DoEvents 'wait for the page to load
    Loop

    'you will have to revise the below lines according to the HTML of the document
    .document.getElementById("username").Value = username 
    .document.getElementById("password").Value = password
    .document.getElementById("login-button").Click
    End With
End Sub
这样(很明显,这是一个想法,您需要处理HTML对象),您将无法硬编码用户名和密码,但可以避免检查Internet Explorer外部线程