2

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?

Olle Sjögren
  • 5,315
  • 3
  • 31
  • 51
user2127981
  • 51
  • 2
  • 4

2 Answers2

3

Solution that worked for me:

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

Note the use of ResponseBody instead of ResponseText

OldNewbie
  • 31
  • 2
1

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.)

Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51
  • Hi Daniel. I have check your suggestion and it provides some progress on problem solution. – user2127981 Mar 03 '13 at 16:34
  • For couple of web pages with the character from screen shot, debuger marks "responseText = oHTTP.responseText" part of the code with, and oHTTP.responseText contains "No mapping character..." – user2127981 Mar 03 '13 at 16:48
  • And did you try: responseText = StrConv(oHTTP.responseText,vbUnicode) ? – Daniel Dušek Mar 03 '13 at 17:17