2014-09-19 37 views
0

我正在嘗試編寫一個VBA腳本,該腳本轉到我使用數據表維護的網站,並使用網站上的數據更新範圍。該網站受密碼保護。我有整個VBA腳本,除了一塊:除非一個部分:如果密碼已被輸入,並且瀏覽器保存一個cookie,VBA腳本失敗,因爲它試圖輸入一個值不存在的形式。我需要創建一個if語句來檢查是否存在捕獲密碼的表單 - 如果不存在,則跳過將密碼放入表單的代碼部分。來自密碼保護網站的Excel網頁數據(第一次登錄後不需要密碼)

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 

回答

0
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 

不要忘記,如果/您提交表單後,您將需要等待該文檔以試圖讓你想使用的表的引用之前加載。