【问题标题】:Editing HTML Code using Excel VBA使用 Excel VBA 编辑 HTML 代码
【发布时间】:2017-07-17 11:58:17
【问题描述】:

我需要使用 VBA 编辑 html 代码。就编辑文本框的值而言,我实际上得到了这个工作。我的问题是,当我模拟单击“提交”按钮时,会出现新表。网址保持不变,但现在为表格生成了新的 html 代码。我正在尝试从这些表中读取数据,但是当我尝试查询它们时它们似乎不存在。所以我猜我在按下“提交”按钮后需要更新或刷新 IE 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

【问题讨论】:

    标签: html excel html-parsing vba


    【解决方案1】:

    尝试获取指向窗口的新指针。有时这样做会奏效。

    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
    

    【讨论】:

    • 我对你的处理方式有点困惑。你能帮我把它合并到我的代码中吗?我认为您的想法是正确的,我只是无法将您的代码与我的代码结合起来。谢谢
    • 我想我可能已经正确合并了它,但它仍然无法正常工作。
    • 这行不通? Dim ie2 As InternetExplorer Set ie2 = Nothing Set ie2 = FindWindow("URL", "www.google.com") Set html = ie2.Document
    • 您想在按下提交并加载其他表后调用 FindWindow 函数来获取新指针。更新此引用应更新 IE 对象的属性。
    猜你喜欢
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多