With the holidays just around the corner, schedules are becoming increasingly packed. Maybe you’re on the hunt for the perfect gift, or entertaining the slightly overbearing but well-meaning in-laws, or maybe, just maybe, you need to make those last minute travel plans. With all the hustle and bustle, it’s easy to overlook those seemingly insignificant but entirely necessary holiday cards.
As a developer I'm always looking to save time and automate just about everything...even my holiday cards. (Don’t tell my mother she still thinks I spend hours every year making them.) So in the spirit of the giving season, I wanted to share my sample code with you, to get you on your way to automating your holiday cards and back to searching for the perfect stocking stuffers.
STEP 1 – DOWNLOAD OFFICEWRITER:
To get started, you will need to have OfficeWriter installed in order to be able to use the sample code below. In my solution, I used OfficeWriter and its built-in WordTemplate functionality to do a server-side mail merge. If you do not have OfficeWriter, you can download a free evaluation here.
STEP 2 – DOWNLOAD SAMPLE CODE:
Next, you will need the sample code. Download the sample solution here. This solution comes bundled with:
-
Sample code
-
Easily modifiable templates for both holiday cards and mailing labels
-
Dummy data
Open the solution and run the generator to see sample holiday cards and mailing labels like:
SAMPLE OUTPUT:
SAMPLE CODE:
Looking at the sample code you will see that with just about 10 lines of OfficeWriter code I can generate both holiday cards and mailing labels.
[sourcecode language="csharp"]
private void GenerateCards()
{
//Generate Cards
WordTemplate wt = new WordTemplate(); //Create Word Template
wt.Open(Page.MapPath("\\Templates\\HolidayCards.in.docx")); //Open Template File
wt.SetMailMerge(DataGenerator.GetDummyData()); //Read in Data and Perform a Mail Merge
wt.Process(); //Process the file
wt.Save(Page.Response); //Save it back to the context stream so the user is prompted to save the file
}
private void GenerateLabels()
{
//Generate Mailing labels
WordTemplate wt = new WordTemplate(); //Create Word Template
wt.EnableNEXTFields = true;
wt.Open(Page.MapPath("\\Templates\\Labels.in.docx")); //Open Template File
wt.SetMailMerge(DataGenerator.GetDummyData()); //Read in Data and Perform a Mail Merge
wt.Process(); //Process the file
wt.Save(Page.Response); //Save it back to the context stream so the user is prompted to save the file
}
[/sourcecode]
STEP 3 – CUSTOMIZING THE SAMPLE APPLICATION:
CUSTOMIZE THE TEMPLATES:
Now that you have the solution running, you may realize that I am not the most artistic person in the world. You might want to change your holiday card template and mailing label template designs. (And by might I mean you are encouraged to do so.) Navigate to the Templates folder in the solution and modify either:
-
the HolidayCards.in.docx
-
the Labels.in.docx
Or add your own!
USING YOUR OWN DATA:
The code I wrote imports the data from a Comma Separated Values file. With minimal modification you can use your own CSV files. Some common applications that let you export to .CSV files are Outlook, Gmail (use the Outlook CSV file format from Gmail), and many other email and contact management clients.
And that is it! You’re good to go! Now go out and automate and impress your mom with the fancy, new holiday cards you spent little to no time making, all while kicking back with a cold glass of eggnog and relishing the hours you saved. If only the rest of the holidays were this easy to automate.
MORE IDEAS AND WAYS TO USE THIS CODE:
In my solution I used .CSV files to store my contacts, but if you are a company this code can easily be expanded to read your data from your company's database and automates many parts of your company. Think contract generations, invoices, newsletters or just many more holiday cards!