Printing labels from C#

Microsoft word has the ability to create a document for printing labels, I would like a way to access this
from C#, my application stores the details of clients address etc so I want to be able to select a group
of clients from my application then hit a button "print labels" that will open up word populate each label
with the correct details and either print it or just leave word open so I can print it myself.
LVL 6
jkna_gunnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nebulus_Commented:
Here an sample (I work with Word 8):

- first add as reference Word 8.0;
- second you have here an sample:


const int wdAutoPosition = 0;

Word.Application w = new Word.Application();

object isFalse = false;
object isTrue = true;
object Address = "delivery address";
object ReturnAddress = "return address";
object missing = System.Reflection.Missing.Value;
object position = wdAutoPosition;
object height = w.InchesToPoints((float)4.75);
object width = w.InchesToPoints(11);

w.Visible = false;

w.Documents.Add(ref missing,ref missing);

w.ActiveDocument.Envelope.PrintOut(ref isFalse,ref Address,ref missing,ref isFalse,
  ref ReturnAddress,ref missing,ref isFalse,ref isFalse,ref missing,ref height,ref width,ref missing,ref position,
  ref position,ref position,ref position,ref missing,ref missing);

w.Quit(ref isFalse,ref missing,ref missing);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nebulus_Commented:
sorry, this sample is for envelope, for labels:

instead of that big line:

object Name = "2163 Mini";

w.Application.MailingLabel.DefaultPrintBarCode = false;
w.Application.MailingLabel.PrintOut(ref Name,ref Address,ref missing,ref missing,ref missing, ref missing,ref missing);
jkna_gunnAuthor Commented:
Hi, yes that would create the document but how would i populate each individual label with an address?
Nebulus_Commented:
here in the var: object Address = "delivery address"; is stored the address;
and function w.Application.MailingLabel.PrintOut(...) print a label.


I just send to you a sample of how staff works, the syntax of MailingLabel usage is complex.
If you need more a suggestion:

- record macro in MS Word with what you want;
- translate in C# the code as you can see in my sample.
jkna_gunnAuthor Commented:
Sorry for not getting back earlier I have been away.
How do you translate a macro into C# code?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.