2017-07-17 192 views
0

我需要使用VBA編輯HTML代碼。至於編輯文本框的值,我確實得到了這個工作。我的問題是,當我模擬點擊「提交」按鈕時,出現了新的表格。網址保持不變,但現在爲表格生成了新的html代碼。我試圖從這些表中讀取數據,但當我嘗試查詢它們時,似乎它們不存在。所以我猜我需要更新或刷新IE瀏覽器的HTML代碼後,我按「提交」按鈕。我似乎無法弄清楚如何做到這一點。任何幫助是極大的讚賞。這是我的代碼到目前爲止:使用Excel VBA編輯HTML代碼

Sub ImportStackOverflowData() 

Dim SearchFor As IHTMLElement 
Dim RowNumber As Long 
RowNumber = 4 

'to refer to the running copy of Internet Explorer 
Dim ie As InternetExplorer 
'to refer to the HTML document returned 
Dim html As HTMLDocument 
'open Internet Explorer in memory, and go to website 
Set ie = New InternetExplorer 
ie.Visible = True 
ie.Navigate "http://google.com" 
'Wait until IE is done loading page 
Do While ie.ReadyState <> READYSTATE_COMPLETE 
Application.StatusBar = "Trying to go to TRSDataBase ..." 
DoEvents 
Loop 

Set html = ie.Document 
Application.StatusBar = "" 

'clear old data out and put titles in 
Cells.Clear 

Set SearchFor = html.getElementById("ddl_process") 

'if this is the tag containing the question details, process it 
If SearchFor.ID = "ddl_process" Then 

'Replace the value of dl-process with copperhead name 
Call SearchFor.setAttribute("value", "CPHB_FAT") 
Cells(RowNumber, 1).Value = "Successfully replaced ddl_process to : " &  
SearchFor.getAttribute("value") 

'go on to next row of worksheet 
RowNumber = RowNumber + 1 
End If 

Set SearchFor = html.getElementById("txt_startdate") 
If SearchFor.ID = "txt_startdate" Then 

'Replace the value of dl-process with copperhead name 
Call SearchFor.setAttribute("value", "07-07-17") 
Cells(RowNumber, 1).Value = "Successfully replaced startdate to : " &  
SearchFor.getAttribute("value") 

'go on to next row of worksheet 
RowNumber = RowNumber + 1 

End If 

Set SearchFor = html.getElementById("txt_enddate") 
If SearchFor.ID = "txt_enddate" Then 

'Replace the value of dl-process with copperhead name 
Call SearchFor.setAttribute("value", "07-14-17") 
Cells(RowNumber, 1).Value = "Successfully replaced enddate to : " &    
SearchFor.getAttribute("value") 

'go on to next row of worksheet 
RowNumber = RowNumber + 1 

End If 
'find view button and click it 
Set SearchFor = html.getElementById("btn_header") 
If SearchFor.ID = "btn_header" Then 
    SearchFor.Click 
    Cells(RowNumber, 1).Value = "The View Button has been clicked." 
    'go on to next row of worksheet 
    RowNumber = RowNumber + 1 
End If 


'Now get data from table after it loads 
Application.Wait (Now + TimeValue("0:00:20")) 
Set html = ie.Document <----------This is where i am trying to update or refresh my code after it loads with the new tables 

Debug.Print ie.Document.body.innerHTML 
Range("L5").Value = ie.Document.getElementsByTag("table") 
(1).Rows(1).Cells(2).innerText 

回答

0

嘗試獲取一個新的指針窗口。有時候,這就是訣竅。

Public Function FindWindow(SearchBy As String, SearchCriteria As String) As Object 
    Dim Window As Object 

    For Each Window In CreateObject("Shell.Application").Windows 
     If SearchBy = "URL" And Window.LocationURL Like "*" & SearchCriteria & "*" Then 
      Set FindWindow = Window 
      Exit Function 
     ElseIf SearchBy = "Name" And Window.LocationName Like "*" & SearchCriteria & "*" Then 
      Set FindWindow = Window 
      Exit Function 
     End If 
    Next 

    Set FindWindow = Nothing 
End Function 

Sub getNewPointer() 
    Dim ie As InternetExplorer 

    ie.Navigate "www.google.com" 

    'Wait for the page to load and do actions etc 
    'Your code here 
    ' 
    ' 

    'Clear the IE reference 
    Set ie = Nothing 

    'get a new pointer to the window of interest 
    'Keep in mind it finds the first matching Window based on your criteria! 
    Set ie = FindWindow("URL", "www.google.ca") 

    'Try getting the property you want 
    Debug.Print 
End Sub 
+0

我對你如何去做這件事有點困惑。你能把它合併到我的代碼中嗎?我認爲你有正確的想法,我只是不能將你的代碼與我的代碼結合起來。謝謝 –

+0

我想我可能已經正確合併,但它仍然無法正常工作。 –

+0

這個工作?昏暗IE2作爲InternetExplorer的 設置IE2 =什麼 設置IE2 = FindWindow函數( 「URL」, 「www.google.com」) 組HTML = ie2.Document –