Use VBA to download/save html file containing unicode characters

jaredaseltzer

New Member
Joined
May 5, 2015
Messages
3
I am trying to download and save hundreds of webpages as html files using the code below. The code works great for pages that contain no unicode special characters, but not for what I need. An example url that I'm trying to programmatically download from within Excel is http://cal1.cn.huc.edu/get_a_chapter.php?file=51006&sub=1&cset=H

but all of the hebrew letters are turning into question marks when I download and save the html using this code:

Code:
Sub Main()
    Dim url as String, fn as String
    url="http://cal1.cn.huc.edu/get_a_chapter.php?file=51006&sub=1&cset=H"
    fn="C:\temp\Joshua-Chapter1.htm"
    CreateFile fn, GetHTML(url)
End Sub

Function GetHTML(URL As String) As String
    Dim objHttp As Object
    
    Set objHttp = CreateObject("MSXML2.XMLHTTP")
    Call objHttp.Open("GET", URL, False)
    Call objHttp.Send("")
    GetHTML = objHttp.ResponseText
End Function


Sub CreateFile(FileName As String, Contents As String)
    ' creates file from string contents
    Dim tempFile As String
    Dim nextFileNum As Long
    
    nextFileNum = FreeFile
    tempFile = FileName
    Open tempFile For Output As #nextFileNum
    Print #nextFileNum, Contents
    Close #nextFileNum


End Sub
How should I change this code so that it does not turn the special unicode characters into question marks?
 

Some videos you may like

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,207
Use the data in ResponseBody, which is an array of bytes, instead of ResponseText. The code changes to:
Code:
Function GetHTML(URL As String) As Byte()
    Dim objHttp As Object
    
    Set objHttp = CreateObject("MSXML2.XMLHTTP")
    objHttp.Open "GET", URL, False
    objHttp.Send ""
    GetHTML = objHttp.ResponseBody
End Function

Sub CreateFile(FileName As String, Contents() As Byte)
    ' creates file from byte array
    Dim nextFileNum As Long
    
    nextFileNum = FreeFile
    Open FileName For Binary Access Write As #nextFileNum
    Put #nextFileNum, , Contents
    Close #nextFileNum
End Sub
 

jaredaseltzer

New Member
Joined
May 5, 2015
Messages
3
Use the data in ResponseBody, which is an array of bytes, instead of ResponseText. The code changes to:
Code:
Function GetHTML(URL As String) As Byte()
    Dim objHttp As Object
    
    Set objHttp = CreateObject("MSXML2.XMLHTTP")
    objHttp.Open "GET", URL, False
    objHttp.Send ""
    GetHTML = objHttp.ResponseBody
End Function

Sub CreateFile(FileName As String, Contents() As Byte)
    ' creates file from byte array
    Dim nextFileNum As Long
    
    nextFileNum = FreeFile
    Open FileName For Binary Access Write As #nextFileNum
    Put #nextFileNum, , Contents
    Close #nextFileNum
End Sub
Thank you John_w. This would have worked, but I found another solution by calling the API "URLDownloadToFile":

Code:
Option Explicit
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Function Download(url As String, fn As String) As Integer


    Download = URLDownloadToFile(0, url, fn, 0, 0)


    If Download <> 0 Then MsgBox "Error: " & Download & "." 


End Function
 

Watch MrExcel Video

Forum statistics

Threads
1,095,186
Messages
5,442,905
Members
405,206
Latest member
Neetish_93

This Week's Hot Topics

  • Copy entire row if CountA <>0 to another sheet
    [B]I want to copy entire row if CountA <>0 for column J7:AM7 (headers on J6:AM6) and so on till the last used cell is column D and paste the...
  • Select last used Row in Table
    I have created a Table in a Worksheet which is locked to prevent user errors and protect formula. Some of the cells require freetext entries which...
  • excel workbook: do not allow certain file name
    Hello all, Don't think this has ever been asked before, but how do I restrict file save [Before_Save Event] if the name of the file being saved...
  • fixing problem autofilter
    hello i need help about my code when i search by code in textbox it doesn't show anything this is my data [ATTACH type="full"...
  • “Weight”
    Hi, i’ve got a long sheet filled with weights such as kg,g,L & ml. i can build a formula to convert kg into g and liter into ml. How ever, my...
  • How to capitalize everything before a certain character?
    In column A, I have some text: Hello good day.mp3 Hello good day.flac etc. I'd like to capitalize everything before the period. I don't need the...
Top