How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Steve Herrod Your Own Question
Steve Herrod
Steve Herrod, Computer Support Specialist
Category: Microsoft Office
Satisfied Customers: 3324
Experience:  Trained in Microsoft Office from 2000 through to latest version 2010.
65126503
Type Your Microsoft Office Question Here...
Steve Herrod is online now
A new question is answered every 9 seconds

Using VB macro in Word 2007 to do a mail merge. Data source

This answer was rated:

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...


Steve Herrod :

Hi, I'll be happy to help with this issue


 

Steve Herrod :

would it be possible to get a copy of the text file to do some testing here with?


 

Steve Herrod :

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.


 

Steve Herrod :

 


 

Steve Herrod :

Cheers


 

Steve Herrod :

Steve


 

Customer:

I have files, but how do I upload them?

Steve Herrod : Hi
Steve Herrod : If you go to wikisend.com and choose upload
Steve Herrod : then you can add the files and let me know the download links it gives you
Customer:

OK. The links are below. Test.doc is the mail merge form (catalog type) and contains the macro. Test.txt is the data file.



http://wikisend.com/download/349156/test.doc


http://wikisend.com/download/232060/test.txt

Steve Herrod :

thanks

Customer:

Hello,



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...



Steve Herrod :

thanks....

Customer:

...



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.



More to come...

Customer:

...



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.



Any questions?


Steve Herrod :

sorry - only just got the messages through

Steve Herrod :

that shoudl be ok, will work on that now and come back to you asap

Steve Herrod :

Trying various ways of inserting an encoding format but no joy so far

Steve Herrod :

still trying though

Customer:

Any luck?

Steve Herrod :

Not so far - trying to force the encoding into the macro doesn't seem to work correctly, whatever method is used

Steve Herrod :

I don't want to give up just yet though...

Customer:

I see that my question is now closed. Does this mean you have given up?

Customer:

Hi Steve,


 


I just telephoned JustAnswer and they re-opened the question. What's up?

Steve Herrod :

Apologies - not sure why it was closed, am still working on this

Steve Herrod :

will send an update later today as planning to crack this in the afternoon

Customer:

Apparently, it closes after a certain amount of time...

Steve Herrod :

ah ok - always thought that was a week without any updates...

Steve Herrod :

nevermind though, will get to it

Steve Herrod :

Haven't found a conclusive way of resolving this

Steve Herrod :

But if you create and run this macro

Steve Herrod :

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

Steve Herrod :

and then run your macro the character appears fine

Steve Herrod :

for some reason I cannot get the macro in your file to accept the Encoding:=msoEncodingISO88591Latin1, _ line

Customer:

I have added this to the beginning of the macro in test.doc:


 

Customer:

I have added this


 

Customer:

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?


 

Customer:

Note that I had to add the ReadOnly parameter...

Steve Herrod :

I think the SET macro just allows a specific encoding variable to be added


 

Customer:

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.

Steve Herrod :

no problem


 

Steve Herrod :

Let me know whenever is convenient


 

Steve Herrod :

Cheers


 

Steve Herrod :

Steve


 

Customer:

Hi Steve,


 

Customer:

msoEncodingWestern works just as well.

Adding the following to my macros makes them work unattended and without any Japanese characters appearing:


 

Steve Herrod :

ok - sounds good


 

Customer:

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.



 

Customer:

In any event, job well done. Thanks.

Steve Herrod :

yes - that makes sense


 

Steve Herrod :

glad you have something which is working


 

Steve Herrod and other Microsoft Office Specialists are ready to help you

Related Microsoft Office Questions