Enforcing Charset for imported Text files into Word

P

Pete

Hi there.

Part of a Word app I'm creating in VBA involves reading and parsing a
text file to transform the contents into a properly styled Word
document.

I'm doing this on a DocumentChange event, where I check the save type
of the file and check the first few characters for a tag before going
on to my transformation code.

However, some of the text files contain non-ASCII characters (open and
close quotes for examples). The problem with this is that Word then
displays a message asking for confirmation of the file type. This
message destroys the event handling so execution is halted at that
time.

I was thinking that if I could MIME encode the text file, I should be
able to bypass the conversion dialog and let the text come through as
UTF-8 or charset iso-8859-1.

Has anyone had an experience doing this, or knows of a way to force
the charset when opening a text file?
 
C

Cindy M.

Hi Pete,

Try asking this in the word.vba.general newsgroup. Look especially for
Klaus Linke, who has a lot of experience with encoding questions.
Part of a Word app I'm creating in VBA involves reading and parsing a
text file to transform the contents into a properly styled Word
document.

I'm doing this on a DocumentChange event, where I check the save type
of the file and check the first few characters for a tag before going
on to my transformation code.

However, some of the text files contain non-ASCII characters (open and
close quotes for examples). The problem with this is that Word then
displays a message asking for confirmation of the file type. This
message destroys the event handling so execution is halted at that
time.

I was thinking that if I could MIME encode the text file, I should be
able to bypass the conversion dialog and let the text come through as
UTF-8 or charset iso-8859-1.

Has anyone had an experience doing this, or knows of a way to force
the charset when opening a text file?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
K

Klaus Linke

Hi Pete,

(Thanks to Cindy for the flowers, but I'm not sure I can live up to
expectations <g>)

You're opening the text file in Word?
Then you should be able to specify the text file, the msoEncoding constant,
and "NoEncodingDialog:=True (and Visible:=False if you want to hide it from
the user):

Set docText = Documents.Open(FileName:="\\Server\data\Test.txt", _
Format:=wdOpenFormatText, _
Encoding:=msoEncodingISO88591Latin1, _
Visible:=False, _
NoEncodingDialog:=True)

You have to figure out what encoding was used, though. Letting Word guess is
pretty risky.

If the file is Unicode (not UTF-8) or Windows CP 1252, using the VB(A) "Open
myFile For Binary As #iFile" might be more "elegant", to quickly get the
contents into a string.
If it's in another encoding, you'd need to do a conversion yourself though,
as far as I know.
Lots of mappings can be found here:
ftp://ftp.unicode.org/Public/MAPPINGS
ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT

Hope I didn't misunderstand what you are trying to do...
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top