2

尝试通过 Excel VBA 接收网站源代码大约需要 4000 个字、40000 个字符。然后它停止。我不知道为什么。

谁能帮我?

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
4

3 回答 3

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
于 2012-07-17T12:04:44.710 回答
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 
于 2014-09-25T11:16:02.923 回答
0

问题是线路

Cells(2, 2) = sTotalData

在 Excel 2007/2010 中,将字符串返回到单元格被限制为 32767 个字符,即使sTotalData比这更长。

参考这个链接

于 2012-07-11T09:48:27.967 回答