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