将一些文本从站点复制到Excel(运行时错误462)

时间:2016-08-16 06:41:25

标签: vba selectors-api

该程序有效(Excel中显示所需的文本)。但之后IE停止工作并运行时错误462(远程服务器机器不存在或不可用)。在互联网上搜索解决方案。 https://support.microsoft.com/en-us/kb/178510。 我无法有效地更改代码。

   Sub extractTablesData1()
Dim IE As Object
Dim Data As Object
Dim xlApp As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlsh As Excel.Worksheet
Dim i As Integer
Dim elemCollection As Variant

Set xlApp = CreateObject("Excel.Application")
Set xlwb = xlApp.Workbooks("IESite.xlsx")
Set xlsh = xlwb.Worksheets("Data")

Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = False
    .navigate ("http://allscores.ru/soccer/new_ftour.php?champ=2404&f_team=406&tour=110")
    While IE.ReadyState <> 4
        DoEvents
    Wend
    Set Data = IE.document.getElementsbyTagName("table")(6).querySelectorAll("td.clr, td.clr_win, td.clr_draw, td.clr_loose")
    i = 1
    For Each elemCollection In Data
        xlsh.Cells(34, 1 + i).Value = elemCollection.innerText
        i = i + 1
    Next elemCollection
End With
IE.Quit
Set IE = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

如链接中所述:

  

解决方案
  要解决此问题,请修改代码,以便使用适当的对象变量限定对Excel对象,方法或属性的每次调用。

您正在使用:

ActiveWorkbook.Sheets(1).Cells(34, 1 + i).Value

这应该是:

Dim xlwb as Workbook
Dim xlsh as Worksheet

set xlwb = Workbooks("IESite")
set xlsh = xlwb.Worksheets("Data")

xlsh.Cells(34, 1 + i).Value = elemCollection.innerText

进一步的变化:

根据解决方案的要求,需要定义所有内容。让我知道如果使用以下方式它确实有效,如果是这样我将删除上面的代码。

不要忘记定义 ElemCollection

Sub extractTablesData1()
    Dim IE As Object
    Dim Data As Object
    Dim xlApp As Excel.Application
    Dim xlwb As Excel.Workbook
    Dim xlsh As Excel.Worksheet
    Dim i As Integer
    Dim elemCollection as ... 'please define elemCollection as the type it is

    Set xlApp = CreateObject("Excel.Application")
    Set xlwb = xlApp.Workbooks("IESite.xlsx")
    Set xlsh = xlwb.Worksheets("Data")

    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = False
        .navigate ("http://allscores.ru/soccer/new_ftour.php?champ=2404&f_team=406&tour=110")
        While IE.ReadyState <> 4
            DoEvents
        Wend
        Set Data = IE.document.getElementsbyTagName("table")(6).querySelectorAll("td.clr, td.clr_win, td.clr_draw, td.clr_loose")
        i = 1
        For Each elemCollection In Data
            xlsh.Cells(34, 1 + i).Value = elemCollection.innerText
            i = i + 1
        Next elemCollection
    End With
    IE.Quit
    Set IE = Nothing
End Sub