2012-07-11 140 views
0

我有一個來自HTML源代碼(大約1,000,000個字符長)的大字符串。我使用msinet.ocx查看適當網站的文本。爲了找到恰好在不同關鍵短語(「Component Accessory Matrix」)之前出現的關鍵短語(「pkid =」),我寫了一小段代碼,但它不能正常工作。這是我現在有的:使用VBA對字符串長度有限制嗎?

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的值,當它應該返回大約一百萬的值時。有人可以解釋這個嗎?

編輯:瘸子,我試過你的解決方案,由於某種原因,ReturnStr仍然是空的。我嘗試了1024而不是2048,但這並沒有改變任何東西。我已經複製並粘貼了我的代碼。

Dim myURL 

ActiveSheet.Range( 「A1」)。完(xlDown)。選擇 lastColumn = Selection.Row

對於得到columnnumber = 2至lastColumn 工作臺=細胞(得到columnnumber,1)。價值 myURL = 「http://beams.us.yazaki.com/Beams/ViewDetails.aspx?topic=document & PKID =」 _ &工作臺 昏暗inet1作爲的Inet 昏暗我的空間作爲字符串 昏暗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函數,我不認爲我在語法上做了任何錯誤,但也許這是一些基本的錯誤。幫助表示讚賞。

+0

@MarkHall .OpenURL確實有限制,而不是字符串。 參考:http://www.vbforums.com/showthread.php?t=54096 發表回答下面。 – danielpiestrak 2012-07-11 19:35:41

+0

這是否意味着你不需要幫助[here](http://stackoverflow.com/q/11416301/190829)? – JimmyPena 2012-07-11 20:37:28

+0

@JimmyPena,我很好。 – sresht 2012-07-13 14:02:40

回答

1

使用iNet,當使用帶有GetChunk功能的iNet OpenURL時,需要以塊的形式讀取文件。

嘗試這樣:

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將會包含整個頁面。

+0

到目前爲止,你能看到我的解決方案嗎?我仍然有麻煩... – sresht 2012-07-13 14:18:37