VBA的字符串长度是否有限制?

时间:2012-07-11 17:14:48

标签: string excel vba excel-vba

我有一个来自HTML源代码的大字符串(大约1,000,000个字符)。我正在使用msinet.ocx查看来自相应网站的文本。我写了一小段代码,以便找到一个关键短语(“pkid =”),它发生在不同的关键短语(“组件附件矩阵”)之前,但它无法正常工作。这就是我现在所拥有的:

workbench = Cells(columnNumber, 1).Value
myURL = "http://beams.us.yazaki.com/Beams/ViewDetails.aspx?topic=document&pkid=" _
& workbench
Dim inet1 As Inet
Dim mypage As String

Set inet1 = New Inet
With inet1
    .Protocol = icHTTP
    .URL = myURL
    mypage = .OpenURL(.URL, icString)
End With

CAMnum = InStr(mypage, "Component Accessory Matrix")
intStart = InStrRev(mypage, "pkid=", CAMnum) + 5
newnum = Mid(mypage, intStart, 6)
Cells(columnNumber, 2).Value = newnum

问题似乎是mypage = .OpenURL(.URL, icString);当我运行len(mypage)时,它返回大约100,000的值,当它应返回大约一百万的值时。有人可以解释一下吗?

编辑:Gimp,我尝试了你的解决方案,由于某种原因,ReturnStr仍然是空的。我也尝试了1024而不是2048,但这并没有改变任何东西。到目前为止,我已经复制并粘贴了我的代码。

Dim myURL

ActiveSheet.Range( “A1”)。完(xlDown)。选择 lastColumn = Selection.Row

对于columnNumber = 2到lastColumn     workbench = Cells(columnNumber,1).Value     myURL =“http://beams.us.yazaki.com/Beams/ViewDetails.aspx?topic=document&pkid=”_     &安培;工作台     Dim inet1作为Inet     Dim mypage As String     Dim ReturnStr As String

Set inet1 = New Inet
With inet1
    .Protocol = icHTTP
    .URL = myURL
    mypage = .OpenURL(.URL, icString)
    ReturnStr = .GetChunk(1024, icString)
End With

Do While Len(ReturnStr) <> 0
    DoEvents
    mypage = mypage & ReturnStr
    ReturnStr = inet1.GetChunk(1024, icString)
Loop

CAMnum = InStr(mypage, "Component Accessory Matrix")
intStart = InStrRev(mypage, "pkid=", CAMnum) + 5
newnum = Mid(mypage, intStart, 6)
Cells(columnNumber, 2).Value = newnum

Next columnNumber

我在这里遗漏了什么吗?我在网上搜索GetChunk函数,我不认为我在语法上做错了什么,但也许这是一些基本的错误。感谢帮助。

1 个答案:

答案 0 :(得分:1)

使用iNet时,需要在使用iNet的OpenURL和GetChunk功能时以块的形式读取文件。

尝试这样的事情:

 myString = iNet1.OpenURL(.url, icString)
 ReturnStr = iNet1.GetChunk(2048, icString)

 Do While Len(ReturnStr) <> 0
    DoEvents
    myString = myString & ReturnStr
    ReturnStr = iNet1.GetChunk(2048, icString)
 Loop

这会将块读入ReturnStr,然后将它们附加到myString的末尾。

在此循环之后,myString将包含整个页面。

相关问题