2012-07-11 72 views
2

尝试通过Excel接收网站源代码VBA适用于大约4000个字,40000个字符。然后停止。我不知道为什么。Excel VBA无法读取超过40,000个字符的网页源

任何人都可以帮助我吗?

Option Explicit 
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000 
Private Declare Function InternetOpen Lib "Wininet.dll" Alias "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long 
Private Declare Function InternetReadFile Lib "Wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer 
Private Declare Function InternetOpenUrl Lib "Wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sUrl As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long 
Private Declare Function InternetCloseHandle Lib "Wininet.dll" (ByVal hInet As Long) As Integer 

Public Sub GetWebPageData() 

    Dim hInternet, hSession, lngDataReturned As Long 
    Dim iReadFileResult As Integer 
    Dim sBuffer As String * 64 
    Dim sTotalData As String 
    Dim sUrl As String 
    Dim sLine As String 

    sUrl = "http://www.engadget.com/" 'Long Website here 
    hSession = InternetOpen("", 0, vbNullString, vbNullString, 0) 

    If hSession Then hInternet = InternetOpenUrl(hSession, sUrl, vbNullString, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0) 

    If hInternet Then 
    iReadFileResult = InternetReadFile(hInternet, sBuffer, 128, lngDataReturned) 

    sTotalData = sBuffer 

    Do While lngDataReturned <> 0 
     iReadFileResult = InternetReadFile(hInternet, sBuffer, 128, lngDataReturned) 
     sTotalData = sTotalData + Mid(sBuffer, 1, lngDataReturned) 
    Loop 
    End If 

    iReadFileResult = InternetCloseHandle(hInternet) 

    'WEBPAGE loaded into sTotalData 
    Cells(2, 2) = sTotalData 
End Sub 
+0

我好奇,为什么你使用这种方法,我使用标准的XMLHTTP请求查询同一页面,速度更快,最终字符串的数据量是原来的两倍,这些代码返回的数据在我的机器上没有意义,tho呃,我使用64位,如果这有什么区别。 – SWa 2012-07-11 13:07:40

+0

你能告诉我任何代码吗?我刚刚发现了两种方法(1.在VBA中打开IExplorer,等待并关闭,非常慢,2.我的方法)。我很感谢任何帮助加快我的计划;-) – 2012-07-17 09:25:19

回答

0

的问题是该行

Cells(2, 2) = sTotalData 

返回一个字符串转换为电池limnited 32767个字符在Excel 2007/2010,即使sTotalData长于。

参考this link

+0

没有时间来证明它,但听起来非常好。感谢你,非常感谢! – 2012-07-17 09:21:31

0

按照要求,得到了完整的源代码的另一种方式:

Function GetSource(url As String) As String 

With CreateObject("MSXML2.XMLHTTP") 
    .Open "GET", url 
    .Send 
    Do: DoEvents: Loop Until .Readystate = 4 
    GetSource = .responsetext 
    .abort 
End With 

End Function 
+0

嘿,我正在寻找一个解决方案来接收超过一周的vba的html源代码。发现速度不够快。而你的代码就像:简单,容易,只有10行代码,简单...非常感谢!希望,寻找的人会找到答案! – 2012-07-18 07:59:12

0

你尝试过:

For i = 1 To Len(sTotalData) Step (Len(sTotalData)/200) '*Line1 of 3 to replace Cells(2, 2) = sTotalData 


Cells(i \ (Len(sTotalData)/200) + 1, "A").Value = "'" & Mid(sTotalData, i, (Len(sTotalData)/200)) '*Line2 of 3 to replace Cells(2, 2) = sTotalData 


Next i '*Line3 of 3 to replace Cells(2, 2) = sTotalData