sresht
sresht

Reputation: 383

Is there a limitation to the string length with VBA?

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.

Upvotes: 0

Views: 1743

Answers (1)

danielpiestrak
danielpiestrak

Reputation: 5439

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.

Upvotes: 1

Related Questions