Is there a limitation to the string length with VBA?

不羁岁月 提交于 2020-01-16 19:25:29

问题


I have a large string that comes from an HTML source code (approximately 1,000,000 characters long). I'm using msinet.ocx to view the text from appropriate websites. I've written a small segment of code in order to find a key phrase ("pkid=") that occurs right before a different key phrase ("Component Accessory Matrix"), but it's not working properly. Here's what I have right now:

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

The problem seems to be with mypage = .OpenURL(.URL, icString); when I run len(mypage), it returns a value of approximately 100,000, when it should be returning a value of about a million. Can someone explain this?

EDIT: Gimp, I tried your solution, and for some reason, the ReturnStr is still empty. I tried 1024 instead of 2048 as well, but that didn't change anything. I've copied and pasted my code so far.

Dim myURL

ActiveSheet.Range("a1").End(xlDown).Select lastColumn = Selection.Row

For columnNumber = 2 To lastColumn 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 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

Am I missing something here? I searched online for the GetChunk function, and I don't think I'm doing anything wrong syntactically, but maybe it's some fundamental error. Help is appreciated.


回答1:


Using iNet, you need to read the file in chunks when using iNet's OpenURL with the GetChunk function.

Try something like this:

 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

This will read the chunks into ReturnStr and then append them to the end of myString.

After this Do loop your myString will have the entire page in it.



来源:https://stackoverflow.com/questions/11437911/is-there-a-limitation-to-the-string-length-with-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!