Problems with mail merge (encoding)

T

Thierry Thoua

Hello,

I've a "little" problem with Word interop (Office 11). I'd like to
automate the mailmerge.... I've a word file and a .txt file (a csv).
The text file is in UTF-8 encoding.

This is my "code" :

this.document.MailMerge.MainDocumentType =
WdMailMergeMainDocType.wdFormLetters;
this.document.MailMerge.Destination =
WdMailMergeDestination.wdSendToNewDocument;
object subType = WdMergeSubType.wdMergeSubTypeOther;
object format = WdOpenFormat.wdOpenFormatUnicodeText;
object confirmConversions = false;
this.document.MailMerge.OpenDataSource(cheminFichier, ref
format, ref confirmConversions, ref ComWrapper.OpenDocumentAsReadOnly,
ref
ComWrapper.Missing, ref ComWrapper.Missing, ref ComWrapper.Missing,
ref ComWrapper.Missing,
ref
ComWrapper.Missing, ref ComWrapper.Missing, ref ComWrapper.Missing,
ref ComWrapper.Missing,
ref
ComWrapper.Missing, ref ComWrapper.Missing, ref ComWrapper.Missing,
ref subType);

document.MailMerge.Execute(ref ComWrapper.Missing);
object index = 1;
_Document mailMergeResult = this.word.Documents.get_Item
(ref index);

It works fine ... BUT .. Sometimes when I load a lot my service ...
The word doesn't use the "utf-8" encoding ... Word use "Windows
standard (default)" => When I execute the mail merge ... I've a window
with a wrong key "ïMyKey" and not "MyKey" .... What's wrong ??? :(

Do you have any idea ?

Thanks
 
P

Peter Jamieson

!. As far as I know, word takes little or notice of the format parameter
when opening Mail Merge Data Sources. Some other parameters that have an
effect when opening a Document via, e.g. Documents.Open are also ignored
in OpenDataSource. Mailmerge tends to use other methods to try to work
out what kind of file it is dealing with.

2. What is probably happening is that
a. The method Word is using to open the text file is deciding the
encoding is not UTF-8. That might, for example, be because there is a
DefaultCPG setting in the registry (have a look under
HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Word\Options
). There could well be other reasons why the text converter is not
recognising UTF-8 correctly, but I do not know what they might be,.
b. If the UTF-8 file does not have a BOM (Byte Order Mark) at the
beginning, then the name of the first column may be correct
c. if the UTF-8 file has a BOM, then Word will treat the BOM as the
first few characters of th efirst column name (in whatever encoding it
is expecting)

That assumes that Word is opening the file using its text converter,
which is what I /think/ your code will do (it's as if you were doing

OpenDataSource name:="the text file.txt"

) and if you are seeing the encoding dialog box that is almost certainly
what it is doing.

3. See if that leads anywhere first. Otherwise, it may be possible to
force Word to use UTF-8 encoding, but only if the OLE DB text provider
can read your files (maybe not if they have multiline texts or more than
255/256 columns), and you are in a position to create/maintain a couple
of other files.

Peter Jamieson

http://tips.pjmsn.me.uk
 

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