• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

How to print word docs and lables from asp.net

I need an example of how to print the contents of a datagrid to a word document.

For example, if I have a gridview of user details, I need to interate through the grid and add firstname, lastname and address to a word template.
  • 4
  • 4
1 Solution
If you are creating word documents on the server side you will need to be using a third party component. Microsoft do not recommend or support the use of Office on the server for unattended automation.

See the following site for a company who have a third party component


However if you do want to automate office go here for a good beginners guide: -

export to excel is a very common feature and you will find lots of articles for that .. to export to word, its just a matter of changing the content type ..

have a look at this sample for export to word ..
ASP.NET : Export to Excel/Word from Nested GridViews

mugseyAuthor Commented:

The request is that there are about 5 standard MS Word letters and I have to merge names and addresses into the addressee section

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

for that you will need to use the MS word object and parse the template and merge the values ..
have a look here on how to do it

Microsoft Word Documents from ASP.NET
from the above link, In your template, you can define bookmarks so that your automation client can fill in variable text at a specific location in the document, as follows:

object oBookMark = "MyBookmark";
oWordDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";

Trust me, I know from experience, this will not be the first problem you will face if you continue to impliment your solution using this technque. No attempt has been made by microsoft for Word to be used in an unnatended or non-interacive client.

Quote from MS Site:
"Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when run in this environment."

Visit here for a detailed explantion of why it is a bad Idea!
If you want to create docs server side, you will need to find a .NET component for Server side creation of office documents. Some companies with suitable products are: -

Aia Software B.V. - http://www.aia-itp.com
Polar - http://www.polarsoftware.com
SoftArtisans - http://www.softartisans.com
SyncFusion- http://www.syncfusion.com
Keylogix - http://www.activedocs.comback to top
With experience what I can say is that there is always a right of doing things and there are wrong ways .. if you do the thing right, most of the time you will not have any major issues .. but having said that if every program or apllication could be 100% error free, most of us would have been out of our jobs .. :-) .. anyway, I have used office automation during the activex/ASP days and I will not say it was a smooth ride, but we learnt as we wrote the code and managed to get it right .. now with officeXP and XMl support, office automation has actually improved .. so if you can spend the additional money to buy the third party software to do the automation, go ahead and buy it, but do some research on their support because finally even those are some software build by some developers .. just like microsoft ... :-) ..

For some additional information on office automation ..

Binding for Office automation servers with Visual C# .NET

Understanding the Word Object Model from a .NET Developer's Perspective

Automating Office Programs with VB.NET

Develop Microsoft Office solutions with Visual Studio .NET

Some problem that people face due to bad implementation
Office application does not quit after automation from Visual Studio .NET client
Office XP Primary Interop Assemblies Known Issues

links taken from http://www.xtremevbtalk.com/showthread.php?t=160459 .. have a look there for more information


With all due respect, I think this is not a good recommendation to make. I too was involved with office automation "in the early days" and had a large number of successes in doing so. However with the migration towards later versions of office. The number of issues have increased greatly to the point that the defensive coding required  to ensure that a successful office automation can be performed far outweighed the cost of Third Party components or alternative solutions. Have you attempted integration with Office 2007. It's a very scary prospect! I would argue that investing in alternative technologies, as recommended by Microsoft would be the correct approach to take as MS continue to move away from supporting this sort of technique.

In addition to this even in the 'early days' MS automation could only be considered in the scenarios where the traffic was low and the chance of concurrently automation requests was very low.

When Microsoft tell you to use one of there competitors products you should stand up and listen as you can be dam sure they wouldn't be doing it unless there was some really big problems with there own!
>>I think this is not a good recommendation to make
I think there no point arguing about this in this forum .. I made my recommendation and you have made yours and mugsey will make up his mind depending on all those factors that affect a typical project like
- is this a project or is this just something he is trying to learn (maybe homework for some college project)
- is this project critical
- has he managed to find some third party software that his orgnisation is using for similar functionality in other project and can he reuse it, do they have the extra licenses
- is this for a client or his own organisation (in-house development),
- does he have other decision makers in his project like a manager and if yes what are his views on this,
- if this is build for a client will the client buy the 3rd party software that mugsey minght recommend, and who will be supporting his application after release
- does he have to build a proof of concept and document all the pros and cons of each of those third party softwares before making a recommendation
- does he have time to evaluate the third party softwares or he have the man power to get it done
- how is the third party support
- does he have the skills (team) to code and maintain a application using office automation,
- is there any other alternative to the functionality he is trying to build,
- are the users ready to have this functionality removed from the project and take a alternative approach (maybe pdf or something like that)
etc etc ..

mugsey is well aware of his situation than we are and he is well suited to take the decision ..

and yes, I was involved in the upgrading, enhancing and maintaining a project developed for one of the leading stock broker in US (500+ users) using office automation .. the project is still live and all those 500+ users are still using the application daily :-) .. and as I said, its not 100% error free .... after release, they have their own support team (helpdesk) who are also involved in supporting their applications .. and my team gets involved in enhancements and defect resolution (if any) ..


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now