运行时错误' -2147467259,自动化错误,未指定错误VBA宏通过IE登录

时间:2015-06-25 10:11:55

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

每当我运行VBA宏以通过IE自动登录到SUGARCRM时,我都会遇到此类错误。

下面的代码。

'Needs references to Microsoft HTML Object Library and Microsoft Internet Controls

Option Explicit

Sub Test()


Const cURL = "http://sam.selexgalileo.com/SugarCRM/index.php?action=Login&module=Users&login_module=Accounts&login_action=index"
Const cUsername = "robertopecora"    'REPLACE XXXX WITH YOUR USER NAME
Const cPassword = "rp3c0r4!!"    'REPLACE YYYY WITH YOUR PASSWORD

Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UserNameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim SignInButton As HTMLInputButtonElement
Dim HTMLelement As IHTMLElement

Set IE = New InternetExplorer

IE.Visible = True
IE.navigate cURL

'Wait for initial page to load

Do While IE.readyState <> READYSTATE_COMPLETE Or IE.busy: DoEvents:

Loop

Set doc = IE.document

'Get the only form on the page

Set LoginForm = doc.forms(0)

'Get the User Name textbox and populate it
'< input name="ctl00$ct$UserName" type="text" maxlength="30" id="ctl00_ct_UserName" style="width:160px;" />

Set UserNameInputBox = LoginForm.elements("ctl00$ct$UserName")
UserNameInputBox.Value = cUsername

'Get the password textbox and populate it
'< input name="ctl00$ct$Password" type="password" maxlength="30" id="ctl00_ct_Password" style="width:160px;" />

Set PasswordInputBox = LoginForm.elements("ctl00$ct$Password")
PasswordInputBox.Value = cPassword

'Get the form input button and click it
'< input type="submit" name="ctl00$ct$uxBtnLogin" value="Sign In" o n c l i c k="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ct$uxBtnLogin", "", true, "Login", "", false, false))" id="ctl00_ct_uxBtnLogin" />

Set SignInButton = LoginForm.elements("ctl00$ct$uxBtnLogin")
SignInButton.Click

'Wait for the new page to load

Do While IE.readyState <> READYSTATE_COMPLETE Or IE.busy: DoEvents: Loop

'Get the HTML document of the new page

Set doc = IE.document

'Determine whether login succeeded or not

If InStr(doc.body.innerText, "Invalid Login Information") = 0 Then
    MsgBox "Login succeeded"
Else
    MsgBox "Login failed"
End If

Debug.Print "Current URL: " & IE.LocationURL


End Sub

0 个答案:

没有答案
相关问题