adodb.stream 在上传下载中可以进行 charset , 正合吾意, 马上试试,果然成功.不要用 StrConv(InputB$(LOF(hFile), #hFile), vbUnicode) 这种方式来解决问题,因为你打开的文本编码是不确定的,另外还存在大容量文本的问题.现归纳成如下函数,该函数能解决由于 utf-8,unicode,ansi,..... 等任何已知编码出现的乱码问题. 'http://spaces.msn.com/members/standardtrip Function UTFDecode(filePathName As String, writeCode As String, readCode As String) As String Dim adoSd As ADODB.Stream Set adoSd = New ADODB.Stream ' adoSd.Mode = adModeRecursive adoSd.Type = adTypeText adoSd.Open adoSd.Charset = writeCode 'UTF-8
adoSd.LoadFromFile filePathName adoSd.Position = 0 adoSd.Charset = readCode 'gb2312 UTFDecode = adoSd.ReadText adoSd.Close Set adoSd = Nothing End Function
该网页为法文,所以用utf-8编码应该可以
'请直接调用TestReg过程 Private Sub TestReg() Dim strData As String strData = getHtmlStr("http://www2.hortilien.com/04_annuaire/fiche_collectivite.htm?cle=1001") msgbox strData End SubPublic Function getHtmlStr(strUrl As String) As String Dim XmlHttp As Object Set XmlHttp = CreateObject("Microsoft.XMLHTTP")
XmlHttp.Open "GET", strUrl, False On Error GoTo Err_net XmlHttp.send
Set XmlHttp = Nothing Err_net: End Function Private Function BytesToBstr(strBody, codeBase) As String Dim objStream As Object Set objStream = CreateObject("Adodb.Stream") objStream.Type = 1 objStream.Mode = 3 objStream.Open objStream.Write strBody objStream.position = 0 objStream.Type = 2 objStream.Charset = codeBase BytesToBstr = objStream.ReadText objStream.Close Set objStream = Nothing End Function
有的字符读不出来: <BR>99, rue du Gnral de Gaulle <BR>17139 Dompierre-sur-Mer <BR>Tl.: 05 46 35 30 14 <BR> Fax: 05 46 35 39 32<BR>应该是: 99, rue du Général de Gaulle 17139 Dompierre-sur-Mer Tél.: 05 46 35 30 14 Fax: 05 46 35 39 32
自己搞定了。 Sub Test() With CreateObject("Microsoft.XMLHTTP") .Open "GET", "http://www2.hortilien.com/04_annuaire/fiche_collectivite.htm?cle=1001", False .send Debug.Print StrConv(.responseBody, vbUnicode, 9) End With End Sub
看看控制面板的区域和语言选项,代码页 28591 开始的这几个,对应了 ISO 8859,是否选上了。
'http://spaces.msn.com/members/standardtrip
Function UTFDecode(filePathName As String, writeCode As String, readCode As String) As String
Dim adoSd As ADODB.Stream
Set adoSd = New ADODB.Stream
' adoSd.Mode = adModeRecursive
adoSd.Type = adTypeText
adoSd.Open
adoSd.Charset = writeCode 'UTF-8
adoSd.LoadFromFile filePathName
adoSd.Position = 0
adoSd.Charset = readCode 'gb2312
UTFDecode = adoSd.ReadText
adoSd.Close
Set adoSd = Nothing
End Function
Private Sub TestReg()
Dim strData As String
strData = getHtmlStr("http://www2.hortilien.com/04_annuaire/fiche_collectivite.htm?cle=1001")
msgbox strData
End SubPublic Function getHtmlStr(strUrl As String) As String
Dim XmlHttp As Object
Set XmlHttp = CreateObject("Microsoft.XMLHTTP")
XmlHttp.Open "GET", strUrl, False
On Error GoTo Err_net
XmlHttp.send
getHtmlStr = BytesToBstr(XmlHttp.ResponseBody, "UTF-8")
Set XmlHttp = Nothing
Err_net:
End Function
Private Function BytesToBstr(strBody, codeBase) As String
Dim objStream As Object
Set objStream = CreateObject("Adodb.Stream")
objStream.Type = 1
objStream.Mode = 3
objStream.Open
objStream.Write strBody
objStream.position = 0
objStream.Type = 2
objStream.Charset = codeBase
BytesToBstr = objStream.ReadText
objStream.Close
Set objStream = Nothing
End Function
有的字符读不出来:
<BR>99, rue du Gnral de Gaulle <BR>17139 Dompierre-sur-Mer <BR>Tl.: 05 46 35 30 14 <BR> Fax: 05 46 35 39 32<BR>应该是:
99, rue du Général de Gaulle
17139 Dompierre-sur-Mer
Tél.: 05 46 35 30 14
Fax: 05 46 35 39 32
Sub Test()
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", "http://www2.hortilien.com/04_annuaire/fiche_collectivite.htm?cle=1001", False
.send
Debug.Print StrConv(.responseBody, vbUnicode, 9)
End With
End Sub
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../css/style1.css" rel="stylesheet" type="text/css">
</head>
iso-8859-1选上了,但是是灰色的。IE打开后页面显示空白,但右键可看到源代码
刚发现老兄的这一句:
getHtmlStr = BytesToBstr(XmlHttp.ResponseBody, "UTF-8")
改成
getHtmlStr = BytesToBstr(XmlHttp.ResponseBody, "iso-8859-1")
显示就正常了
应该还是设置的问题。
试试在 IE 选项的语言中加入法语。
关闭所有的插件
我这儿使用webbrowser可以打开的。