使用excelvba编辑HTML代码

使用excelvba编辑HTML代码,html,excel,html-parsing,vba,Html,Excel,Html Parsing,Vba,我需要编辑html代码使用VBA。实际上,就编辑文本框的值而言,我已经做到了这一点。我的问题是,当我模拟单击“提交”按钮时,会出现新表。web地址保持不变,但现在为表生成了新的html代码。我试图从这些表中读取数据,但当我尝试查询它们时,它们似乎不存在。所以我猜我需要在按下“提交”按钮后更新或刷新IE html代码。我似乎不知道该怎么做。非常感谢您的帮助。以下是我目前的代码: Sub ImportStackOverflowData() Dim SearchFor As IHTMLElement

我需要编辑html代码使用VBA。实际上,就编辑文本框的值而言,我已经做到了这一点。我的问题是,当我模拟单击“提交”按钮时,会出现新表。web地址保持不变,但现在为表生成了新的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
Sub-ImportStackOverflowData()
模糊搜索作为IHTMLElement
暗行数等于长行数
行数=4
'以引用Internet Explorer的运行副本
Dim ie作为InternetExplorer
'以引用返回的HTML文档
将html设置为HTMLDocument
'在内存中打开Internet Explorer,然后转到网站
Set ie=新的InternetExplorer
可见=真实
即“导航”http://google.com"
'等待IE完成加载页面
在ie.ReadyState ReadyState\u完成时执行此操作
Application.StatusBar=“正在尝试转到TRSDataBase…”
多芬特
环
设置html=ie.Document
Application.StatusBar=“”
“清除旧数据并输入标题
牢房,没问题
设置SearchFor=html.getElementById(“ddl_进程”)
'如果这是包含问题详细信息的标记,请处理它
如果SearchFor.ID=“ddl\u进程”,则
'将dl进程的值替换为铜斑蛇名称
调用SearchFor.setAttribute(“value”,“CPHB_-FAT”)
单元格(RowNumber,1).Value=“成功地将ddl_进程替换为:”&
SearchFor.getAttribute(“值”)
'转到工作表的下一行
RowNumber=RowNumber+1
如果结束
设置SearchFor=html.getElementById(“txt\u startdate”)
如果SearchFor.ID=“txt\u startdate”,则
'将dl进程的值替换为铜斑蛇名称
调用SearchFor.setAttribute(“值”,“07-07-17”)
单元格(RowNumber,1)。Value=“成功地将startdate替换为:”&
SearchFor.getAttribute(“值”)
'转到工作表的下一行
RowNumber=RowNumber+1
如果结束
设置SearchFor=html.getElementById(“txt\u enddate”)
如果SearchFor.ID=“txt\u enddate”,则
'将dl进程的值替换为铜斑蛇名称
调用SearchFor.setAttribute(“值”,“07-14-17”)
单元格(RowNumber,1)。Value=“已成功将enddate替换为:”&
SearchFor.getAttribute(“值”)
'转到工作表的下一行
RowNumber=RowNumber+1
如果结束
'查找视图按钮并单击它
设置SearchFor=html.getElementById(“btn_头”)
如果SearchFor.ID=“btn\u header”,则
搜索。单击
单元格(RowNumber,1)。Value=“已单击查看按钮。”
'转到工作表的下一行
RowNumber=RowNumber+1
如果结束
'现在在加载表后从表中获取数据
Application.Wait(现在+时间值(“0:00:20”))

设置html=ie.Document尝试获取指向窗口的新指针。有时候,这就成功了

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在按下submit并加载其他表后,要调用FindWindow函数以获取新指针。更新此引用应更新IE对象的属性。