Word 2007 Mailmerge with some label types doesn't give expected output
A few days ago I encountered this issue and surely it did take me a lot of time to understand what's going wrong and why.
In Word 2007 handling of labels is changed totally, now it reads label data form the XML files in "%Program Files%\Microsoft Office\Office12\PAGESIZE".
Do you remember the dialog box below ? Yes, that's what I am talking about -- the values for this dialog box are pulled from a set of XML files that are outside word. But, That's good. Extensibility ..keeping XML files outside the program. Easy reusability ... reading the labels data without automating word is good. But, if its all good ...why its an issue ? why people are complaining ? where is the problem.
Read on ..
There is nothing wrong with the approach. But, these XML files are given by the vendors (label vendors). Although the files are checked for the schema validation and many other things, but finally vendors are the once who will give names to there products ..this introduces a limitation/dependency on the vendor that they give/choose unique names for their products.
Word API to mailmerge for the labels is “Application.MailingLabel.CreateNewDocument” only takes one parameter to identify the label ( “name” ). So, in order to identify the label correctly it’s obviously dependent a unique names. Now if there are two labels with the same name in different categories, Word has no way to know which label do you want to print and it may or may not choose the one that you want.
Putting the issue in simplest words -- "Using the sample from KB article - How to automate Word from Visual Basic to create a mail merge for mailing labels fails for some of the labels in Word 2007 (e.g. Avery - 5163, 5164,5146,5147 etc. ).
After some researching I realized that there is one more API that we can use in order to avoid the above problem. CreateNewDocumentByID which takes a label id as an argument. But, now the problem is how to know the label id ? There are two ways (that I know of ). First is to record a macro, which will (may..should) show you the label id. Usually, this won't be really useful unless you want your program to work with only one type of label.
The other way is -- Parsing XML files and extracting vendor ids and product id, once you have these. Use ""Label ID = (vendor id << 24) | product id" to calculate the label ID.
Today I am a bit busy .. and wont' be able to post a sample code snippet for this but if I get any feedback on this post about the sample i'll surely write a sample and will post it.
Bye for today..
Technorati tags: Pranav Wagh, Microsoft Blogger, Troubleshooting, Word 2007, XML, Sample Code
del.icio.us tags: Pranav Wagh, Microsoft Blogger, Troubleshooting, Word 2007, XML, Sample Code
Comments
Anonymous
August 13, 2007
A few days ago I encountered this issue and surely it did take me a lot of time to understand what'sAnonymous
November 30, 2007
Hello. Thank you for this very informative article. If you ended up creating a code snippet that demonstrates how to get the label ID, this would be very much appreciated.Anonymous
March 31, 2008
Did you ever come up with a code solution for this problem? ThanksAnonymous
February 12, 2009
Pranav, Would you be able to post your sample, so far I have been able to extract the label id from a macro but have not been able to retreive it from the xml files Will do it, give me some time to find it out!Anonymous
November 02, 2009
A quick fix that worked for me is to find the xml files that feature the product name you need and delete the one(s) you don't need. In my case, I deleted the file related to A4 paper size and kept the one for Letter.