简体   繁体   English

来自受密码保护的站点的Excel Web数据(首次登录后不需要密码)

[英]Excel web data from password protected site (password not required after first login)

I am trying to write a VBA script that goes to a website I maintain with a table of data and updates a range with the data on the website. 我正在尝试编写一个VBA脚本,该脚本将通过数据表访问我维护的网站,并使用该网站上的数据更新范围。 The website is password protected. 该网站受密码保护。 I have the whole VBA script figured out except for one piece: if the password has already been input and the browser saves a cookie, the VBA script fails because it tries to input a value into a form that doesn't exist. 我已经弄清楚了整个VBA脚本,除了以下一项:如果已经输入了密码并且浏览器保存了cookie,则VBA脚本会失败,因为它试图将值输入到不存在的表单中。 I need to create an if statement that checks if the form that catches the password exists - and if it doesn't, skips the section of code that puts the password into the form. 我需要创建一个if语句,以检查捕获密码的表单是否存在-如果不存在,则跳过将密码放入表单的代码部分。

Sub GetTable()

Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject

'create a new instance of ie
Set ieApp = New InternetExplorer

'you don’t need this, but it’s good for debugging
ieApp.Visible = True

'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "http://google.com/"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

Set ieDoc = ieApp.Document

'fill in the login form – View Source from your browser to get the control names
    Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
MsgBox ieDoc.getElementById("pwbox-106")

If ieDoc.getElementById("pwbox-106") <> Null Then
   With ieDoc.forms("The form that accepts and submits password")
    .post_password.Value = "PASSWORD"
    .submit
End With

End If

'get the table based on the table’s id
Set ieDoc = ieApp.Document
Set ieTable = ieDoc.all.Item("tablepress-1")

'copy the tables html to the clipboard and paste to the sheet
If Not ieTable Is Nothing Then
    Set clip = New DataObject
    clip.SetText "<html>" & ieTable.outerHTML & "</html>"
    clip.PutInClipboard
    Sheet2.Select
    Sheet2.Range("A1").Select
    Sheet2.PasteSpecial "Unicode Text"
End If

'close IE
ieApp.Quit
Set ieApp = Nothing

End Sub
Dim el

Set el = ieDoc.getElementById("pwbox-106")

If Not el Is Nothing Then
   With ieDoc.forms("The form that accepts and submits password")
    .post_password.Value = "PASSWORD"
    .submit
   End With
End If

Don't forget that if/after you submit the form, you will need to wait for the document to load before trying to get a reference to the table you want to use. 不要忘记,如果/在提交表单之后,您将需要等待文档加载后才能尝试获取要使用的表的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM