excel - Convert UTF-8 to ANSI using VBA -
i have vba excel code takes japanese data excel sheet compares japanese data in text file , replaces japanese words english words. supposed able on utf-8 text file. code replaces japanese words weird characters. how save without issue ?
open sfilename input ifilenum n = 1 lnglastcell label5.caption = n & "/" & lnglastcell searchtext = mysearch(n) valuetext = mytext(n) eplccount = 0 spltcount = 0 searchpart = array(searchtext) valuepart = array(valuetext) until eof(ifilenum) line input #ifilenum, sbuf stemp = stemp & sbuf & vbcrlf loop close ifilenum stemp = replace(stemp, searchtext, valuetext) 'ifilenum = freefile open sfilename output ifilenum print #ifilenum, stemp next n
code works ansi characters.
the open
function vba works on ansi
encoded files , binary. if wish read/write utf-8
file, you'll have find way.
the utf-8
encoding has larger set of characters ansi
, it's not possible convert ansi
utf-8
without loss. said, string
in excel , vba stored utf-16
(vba editor still use ansi
), need convert utf-8
utf-16
.
with adodb.stream
:
public function readfile(path string, optional charset string = "utf-8") static obj object if obj nothing set obj = vba.createobject("adodb.stream") obj.charset = charset obj.open obj.loadfromfile path readfile = obj.readtext() obj.close end function public sub writefile(path string, text string, optional charset string = "utf-8") static obj object if obj nothing set stream = vba.createobject("adodb.stream") obj.charset = charset obj.open obj.writetext text obj.savetofile path obj.close end sub
Comments
Post a Comment