Адаптация программного кода (windows 7) к windows 10 / office 10 - PullRequest
0 голосов
/ 01 апреля 2019

У меня есть программа (синтаксический анализ сайта), которая отлично работает на Windows 7.Обращаясь к windows 10 / office 10, выяснилось, что MSXML больше не поддерживается, и мне нужно переписать программу.При первой попытке переписать код появляется сообщение об ошибке:

Ошибка времени выполнения '-2147467259 (80004005)': ошибка автоматизации Неуказанная ошибка

В строке:

Set objIE = New InternetExplorer

Старый код:

Function extractTable(Ssilka As String, book1 As Workbook, iLoop As Long)
   Dim oDom As Object, oTable As Object, oRow As Object
   Dim iRows As Integer, iCols As Integer
   Dim x As Integer, y As Integer
   Dim data()
   Dim vata()
   Dim tata()
   Dim oHttp As Object
   Dim oRegEx As Object
   Dim sResponse As String
   Dim oRange As Range
   Dim odRange As Range

' get page
Set oHttp = CreateObject("MSXML2.XMLHTTP")
oHttp.Open "GET", Ssilka, False
oHttp.Send

' cleanup response
sResponse = StrConv(oHttp.responseBody, vbUnicode)
Set oHttp = Nothing

sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))

Set oRegEx = CreateObject("vbscript.regexp")
With oRegEx
    .MultiLine = True
    .Global = True
    .IgnoreCase = False
    .Pattern = "<(script|SCRIPT)[\w\W]+?</\1>"
    sResponse = .Replace(sResponse, "")
End With
Set oRegEx = Nothing

' create Document from response
Set oDom = CreateObject("htmlFile")
oDom.Write sResponse
DoEvents

' table with results, indexes starts with zero
Set oTable = oDom.getelementsbytagname("table")(3)

DoEvents

iRows = oTable.Rows.Length
iCols = oTable.Rows(1).Cells.Length

' first row and first column contain no intresting data
ReDim data(1 To iRows - 1, 1 To iCols - 1)
ReDim vata(1 To iRows - 1, 1 To iCols - 1)
ReDim tata(1 To iRows - 1, 1 To iCols - 1)
' fill in data array
For x = 1 To iRows - 1
    Set oRow = oTable.Rows(x)

    For y = 1 To iCols - 1
         If oRow.Cells(y).Children.Length > 0 Then
            data(x, y) = oRow.Cells(y).getelementsbytagname("a")(0).getattribute("href")
                data(x, y) = Replace(data(x, y), "about:", "http://allscores.ru/soccer/")
            vata(x, y) = oRow.Cells(y).innerText

        End If

    Next y
Next x

Set oRow = Nothing
Set oTable = Nothing
Set oDom = Nothing

Set oRange = book1.ActiveSheet.Cells(110, 26 + (iLoop * 21)).Resize(iRows - 1, iCols - 1)
oRange.NumberFormat = "@"
oRange.Value = data

Set odRange = book1.ActiveSheet.Cells(34, 26 + (iLoop * 21)).Resize(iRows - 1, iCols - 1)
odRange.NumberFormat = "@"
odRange.Value = vata

Set oRange = Nothing
Set odRange = Nothing

End Function

Новый код:

  Function extractTable(Ssilka As String, book1 As Workbook, iLoop As Long)
  Dim oTable As Object, oRow As Object
  Dim iRows As Integer, iCols As Integer
  Dim x As Integer, y As Integer
  Dim data()
  Dim vata()
  Dim tata()
  Dim oRange As Range
  Dim odRange As Range
  Dim objIE As InternetExplorer 'special object variable representing the IE browser

 'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer

'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = False

'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate Ssilka

'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

' table with results, indexes starts with zero
Set oTable = objIE.document.getElementsByTagName("table")(3)

DoEvents

iRows = oTable.Rows.Length
iCols = oTable.Rows(1).Cells.Length

' first row and first column contain no intresting data
ReDim data(1 To iRows - 1, 1 To iCols - 1)
ReDim vata(1 To iRows - 1, 1 To iCols - 1)
ReDim tata(1 To iRows - 1, 1 To iCols - 1)
' fill in data array
For x = 1 To iRows - 1
    Set oRow = oTable.Rows(x)

    For y = 1 To iCols - 1
         If oRow.Cells(y).Children.Length > 0 Then
            data(x, y) = oRow.Cells(y).getElementsByTagName("a")(0).getattribute("href")
                data(x, y) = Replace(data(x, y), "about:", "http://allscores.ru/soccer/")
            vata(x, y) = oRow.Cells(y).innerText

        End If

    Next y
Next x

Set oRow = Nothing
Set oTable = Nothing

Set oRange = book1.ActiveSheet.Cells(110, 26 + (iLoop * 21)).Resize(iRows - 1, iCols - 1)
oRange.NumberFormat = "@"
oRange.Value = data

Set odRange = book1.ActiveSheet.Cells(34, 26 + (iLoop * 21)).Resize(iRows - 1, iCols - 1)
odRange.NumberFormat = "@"
odRange.Value = vata

Set oRange = Nothing
Set odRange = Nothing
 'close the browser
objIE.Quit
End Function

Ответы [ 2 ]

2 голосов
/ 01 апреля 2019

Вместо того, чтобы переписывать его для использования Internet Explorer, вы можете просто обновить свою ссылку.Вы использовали MSXML2.XMLHTTP, которого больше нет в Windows 10, поскольку оно было обновлено до более поздней версии.Попробуйте использовать MSXML2.XMLHTTP.6.0 вместо

0 голосов
/ 02 апреля 2019

1) Инструменты - Ссылки (Выберите Microsoft XML, v6.0)

2) Изменить строки:

Dim oHttp As MSXML2.XMLHTTP60

Установить oHttp = CreateObject("MSXML2.XMLHTTP.6.0")

Теперь все работает.Спасибо!

...