【问题标题】:Is there a limitation to the string length with VBA?VBA 对字符串长度有限制吗?
【发布时间】:2012-07-11 17:14:47
【问题描述】:

我有一个来自 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").End(xlDown).Select lastColumn = Selection.Row

对于 columnNumber = 2 到 lastColumn 工作台 = Cells(columnNumber, 1).Value myURL = "http://beams.us.yazaki.com/Beams/ViewDetails.aspx?topic=document&pkid=" _ & 工作台 将 inet1 调暗为 Inet 将 mypage 调暗为字符串 将 ReturnStr 调暗为字符串

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 函数,我认为我在语法上没有做错任何事情,但也许这是一些基本错误。感谢您的帮助。

【问题讨论】:

标签: string vba excel


【解决方案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 的末尾。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 2011-04-19
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多