views:

217

answers:

3

I need a way of generating a word document (from a template or something) and inserting an image at a specific place. Does anyone have any pointers on the best way to do this?

I worked on a project that used Office Automation in .NET 1.1 a few years ago, and it was really unspeakably poor. I'm assuming OA has either been improved or been superceeded by a better solution, but I'm not finding much advice on google.

Edit to clarify, this will be running on a web or sharepoint server

A: 

Following line would add the image to word document.

wordDoc.InlineShapes.AddPicture(filePath, ref  link, ref  save, ref  range);

Here, link should be false and save should be true. Range should be the location where you need to add image.

This link should help out dealing with the Interop.

danish
+2  A: 

Alternatively, and if you do not need to generate word documents that will work with versions prior to Word 2007, you could use the OpenXML SDK to create your Word Document. It's all managed code and way easier in my opinion to use than OA.

Gimly
thanks, had not heard of OpenXML.
fearofawhackplanet
+2  A: 

Having done something very similar, I would advise against it. Office Automation within a server environment is buggy. Further the COM Interop requires an interactive user, i.e. there is no 'headless' mode.

Use OpenXML as suggested by Gimly, this would be cleaner approach.

Darknight