Using VB macro in Word 2007 to do a mail merge. Data source is a text file (.txt). When the macro runs it assumes the text file is using Shift-JIS (Japanese) encoding and any accented characters (e.g. é) are combined with the following character and converted to a Japanese character. Have tried using: .OpenDataSource Format:=wdOpenFormatText, ConfirmConversions:=False and variants to no avail.
When I do the mail merge manually it also defaults to Shift-JIS, but it gives me the "File Conversion" dialog box in which I can change to "Windows (Default)" encoding. That solves the problem, but I need the macro to run without user intervention.
I can send files if you want...
Hi, I'll be happy to help with this issue
would it be possible to get a copy of the text file to do some testing here with?
There are a few issues surrounding this type of operation but with the source file it should be possible to get a macro workaround so that user intervention isn't required.
I have files, but how do I upload them?
OK. The links are below. Test.doc is the mail merge form (catalog type) and contains the macro. Test.txt is the data file.
Here is some clarification.
Open test.doc and run the macro "test". It should perform the merge and then close test.doc, leaving the new catalog open. When I do this the LastName in the second record is "For" followed by a Japanese character. As you can see from test.txt, the correct data is "Forêt". The fourth character is Windows ANSI code 234. Word has assumed that the text file is encoded as "Japanese (Shift-JIS)".
More to come...
Note that test.doc is saved as a "normal Word document". This is done so user intervention is not required during macro execution. If test.doc is saved as a mail merge form, then Word prompts to execute the query when test.doc is opened. So, when you try to do the merge manually (see below) and then close test.doc, Word will prompt to save changes. Don't!
Try the merge manually from test.doc. To do this you must first associate the data file ("recipient list" in Word 2007). When I do this I get the "file conversion" dialog. The "other encoding" radio button is selected and "Japanese (Shift-JIS)" is highlighted in the selection box. The preview pane shows the Japanese character. If I change the encoding to, for example, Western European (Windows),Western European (ISO) or Unicode (UTF-7), the data appears correctly in the preview pane.
So to summarize, I am looking for a change in the macro or Word or Windows configuration parameters that will prevent Word from assuming that the encoding is Shift-JIS.
Also, when doing the merge manually, the Windows (default) radio button has the desired result.
The issue only occurs when the data file contains "foreign" characters, i.e. those between code 128 and 255. If there aren't any of these characters in the data file, the macro works perfectly and the "file conversion" dialog does not appear when the merge is attempted manually.
sorry - only just got the messages through
that shoudl be ok, will work on that now and come back to you asap
Trying various ways of inserting an encoding format but no joy so far
still trying though
Not so far - trying to force the encoding into the macro doesn't seem to work correctly, whatever method is used
I don't want to give up just yet though...
I see that my question is now closed. Does this mean you have given up?
I just telephoned JustAnswer and they re-opened the question. What's up?
Apologies - not sure why it was closed, am still working on this
will send an update later today as planning to crack this in the afternoon
Apparently, it closes after a certain amount of time...
ah ok - always thought that was a week without any updates...
nevermind though, will get to it
Haven't found a conclusive way of resolving this
But if you create and run this macro
Set docText = Documents.Open(FileName:="\\Server\data\Test.txt", _Format:=wdOpenFormatText, _Encoding:=msoEncodingISO88591Latin1, _Visible:=False, _NoEncodingDialog:=True)
and then run your macro the character appears fine
for some reason I cannot get the macro in your file to accept the Encoding:=msoEncodingISO88591Latin1, _ line
I have added this to the beginning of the macro in test.doc:
I have added this
I have added this to the beginning of the macro "test" in test.doc:
'set up encoding' Dim docText As Variant Set docText = Documents.Open(FileName:="test.txt", _ ReadOnly:=True, Format:=wdOpenFormatText, _ Encoding:=msoEncodingISO88591Latin1, _ Visible:=False, _ NoEncodingDialog:=True)and now the mailmerge works properly. Why? What does your SET macro do?
Note that I had to add the ReadOnly parameter...
I think the SET macro just allows a specific encoding variable to be added
I need to leave the office now, returning Tuesday AM. I'll test this in a real environment and then we can close the question. Thanks for your help.
Let me know whenever is convenient
msoEncodingWestern works just as well.Adding the following to my macros makes them work unattended and without any Japanese characters appearing:
ok - sounds good
Dim x As Variant Set x = Documents.Open _ (FileName:="OpenDataSourceNameParameter", _ Visible:=False, Encoding:=msoEncodingWestern)I think the root solution lies in the "system code page", whatever that is.
In any event, job well done. Thanks.
yes - that makes sense
glad you have something which is working