Excel 将程序代码(windows 7)改编为windows 10/office 10
我有一个程序(站点解析),可以在Windows7上正常工作。转到Windows10/Office10,它显示MSXML不再受支持,我需要重写该程序。首次尝试重写代码时,出现错误: 运行时错误“-2147467259(80004005)”: 自动化误差 未明错误 一致:Excel 将程序代码(windows 7)改编为windows 10/office 10,excel,vba,parsing,internet-explorer,Excel,Vba,Parsing,Internet Explorer,我有一个程序(站点解析),可以在Windows7上正常工作。转到Windows10/Office10,它显示MSXML不再受支持,我需要重写该程序。首次尝试重写代码时,出现错误: 运行时错误“-2147467259(80004005)”: 自动化误差 未明错误 一致: Set objIE = New InternetExplorer 旧代码: Function extractTable(Ssilka As String, book1 As Workbook, iLoop As Long)
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
函数提取表(Ssilka为字符串,book1为工作簿,iLoop为长)
暗奥多姆为对象,可旋转为对象,或暗奥多姆为对象
Dim iRows为整数,iCols为整数
将x作为整数,y作为整数
Dim数据()
Dim vata()
Dim tata()
作为对象的Dim-oHttp
作为对象的Dim oRegEx
暗响应为字符串
暗橙色作为射程
变光范围
“获取页面
设置oHttp=CreateObject(“MSXML2.XMLHTTP”)
oHttp.打开“获取”,Ssilka,False
oHttp.发送
“清理响应
sResponse=StrConv(oHttp.responseBody,vbUnicode)
设置oHttp=无
sResponse=Mid$(sResponse,InStr)(1,sResponse,"您可以使用MSXML2.XMLHTTP
更新您的引用,而不是使用Internet Explorer重新编写它。您使用的是Windows 10中不再存在的MSXML2.XMLHTTP
,因为它已更新到更高版本。请尝试使用MSXML2.XMLHTTP.6.0
而不是使用Internet Explorer重新编写它,您可以只更新您的引用ur参考。您使用的是Windows 10中不再存在的MSXML2.XMLHTTP
,因为它已更新为更高版本。请尝试改用MSXML2.XMLHTTP.6.0
工具-参考(选择Microsoft XML,v6.0)
2) 换行:
作为MSXML2.XMLHTTP60的Dim oHttp
设置oHttp=CreateObject(“MSXML2.XMLHTTP.6.0”)
现在它起作用了。谢谢大家! 1)工具-参考(选择Microsoft XML,v6.0)
2) 换行:
作为MSXML2.XMLHTTP60的Dim oHttp
设置oHttp=CreateObject(“MSXML2.XMLHTTP.6.0”)
现在它起作用了。谢谢大家! 他正在使用一个迟到的通话,那么这是否仍然适用?它在Windows8版本上没有问题。我无法使用windows 10进行测试,因此最好知道您是否已经进行了测试。他正在使用一个延迟绑定呼叫,那么这是否仍然适用?它在Windows8版本上没有问题。我无法使用windows 10进行测试,因此很高兴知道您是否已经进行了测试。我尝试在windows 10计算机上测试您的新代码。根据我的测试结果,我发现在设置IE对象时没有出现任何错误。如果您遇到错误,请尝试使用其他方法设置IE对象,如set IE=CreateObject(“InternetExplorer.Application”),我建议您首先尝试在该机器上测试简单自动化,然后尝试运行原始代码。我尝试在windows 10机器上测试新代码。根据我的测试结果,我发现在设置IE对象时没有出现任何错误。如果您遇到错误,而不是尝试使用诸如set IE=CreateObject(“InternetExplorer.Application”)之类的替代方法设置IE对象,我建议您首先尝试在该机器上测试简单自动化,然后尝试运行原始代码。
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