2013-03-03 79 views
2

這裏是一個函數的源代碼我使用的進一步等待處理獲取HTML代碼:VBA WINHTTP沒有映射字符

Public Function DownloadTextFile(url As String) As String 
    Dim oHTTP As WinHttp.WinHttpRequest 

    Set oHTTP = New WinHttp.WinHttpRequest 
    oHTTP.Open Method:="GET", url:=url, async:=False 
    oHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" 
    'oHTTP.setRequestHeader "Content-Type", "multipart/form-data; " 
    oHTTP.setRequestHeader "Content-Type", "text/html; charset=utf-8" 
    oHTTP.Option(WinHttpRequestOption_EnableRedirects) = True 
    oHTTP.send 

    Dim success As Boolean 
    success = oHTTP.waitForResponse() 
    If Not success Then 
     Debug.Print "DOWNLOAD FAILED!" 
     Exit Function 
    End If 

    Dim responseText As String 
    Debug.Print oHTTP.responseText 

    responseText = oHTTP.responseText 
    'Set fs = CreateObject("Scripting.FileSystemObject") 
    'Set a = fs.CreateTextFile("c:\testfile.txt", True, False) 
    'Set a = fs.CreateTextFile("c:\testfile.txt", True, True) 
    'a.WriteLine oHTTP.responseText 
    'a.Close 

    Set oHTTP = Nothing 

    DownloadTextFile = responseText 
End Function 

它適用於大多數的網頁,但對於一些頁面responseTextNo Mapping for the Unicode character exists in the target multi-byte code page

這裏是網頁的一個例子的量,responseTextNo Mapping for the Unicode character exists in the target multi-byte code page

http://bzp0.portal.uzp.gov.pl/index.php?ogloszenie=browser&action=search&rodzajzamowienia=B&rodzajogloszenia=1&aktualne=1&datapublikacji_rodzaj=5&iloscwynikownastronie=20&offset=20

,這裏是一個可疑人物不能被編碼(截圖來自谷歌瀏覽器):

http://imageshack.us/photo/my-images/585/errsource.png/

時不時在同一個網站上,但對於不同的搜索結果這個功能不會產生錯誤,但是,然後,在immidi的HTML源代碼吃的窗口就像?????? ...

任何想法如何使它的工作?

回答

1

嘗試使用中StrConv:

DownloadTextFile = VBA.Strings.StrConv(responseText, vbUnicode) 

vbUnicode:使用系統的默認代碼頁的字符串爲Unicode轉換。 (在Macintosh上不可用。)

+0

丹尼爾你好。我已檢查了您的建議,並提供了有關問題解決方案的一些進展。 – user2127981 2013-03-03 16:34:49

+0

對於幾個網頁與屏幕截圖中的字符,debuger會在代碼中標記「responseText = oHTTP.responseText」部分代碼,而oHTTP.responseText則包含「No mapping character ...」 – user2127981 2013-03-03 16:48:46

+0

您是否嘗試過:responseText = StrConv (oHTTP.responseText,vbUnicode)? – dee 2013-03-03 17:17:54

1

解決方案,爲我工作:

responseText = VBA.Strings.StrConv(oHTTP.ResponseBody, vbUnicode) 

的使用注意事項ResponseBody的,而不是responseText的