user2127981
user2127981

Reputation: 51

VBA WinHttp no mapping character

Here is the source of a function I use get HTML code for further proccessing:

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

It works for most pages, but for some pages the responseText is No Mapping for the Unicode character exists in the target multi-byte code page.

Here is an example of web page for which the responseText is No 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

and here is a suspicious character that can't be encoded (screenshot from google chrome):

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

From time to time on same website but for different search result this function does not generate error, but then, the HTML source in immidiate window is like ?????? ...

Any ideas how to make it work?

Upvotes: 2

Views: 3525

Answers (2)

OldNewbie
OldNewbie

Reputation: 31

Solution that worked for me:

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

Note the use of ResponseBody instead of ResponseText

Upvotes: 3

gembird
gembird

Reputation: 14053

Try to use StrConv:

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

vbUnicode: Converts the string to Unicode using the default code page of the system. (Not available on the Macintosh.)

Upvotes: 1

Related Questions