2

I have a .net web application written in C# that is only used on an Intranet.

I have a page on which I need to create a Word document, modify some of the text dynamically, save the Word document and send it as an email attachment.

I tried doing it using Word Automation and, although it worked on my development machine, it doesn't work on a server when other users try to use it.

So ... how to create a Word document on the fly, save it and use it as an email attachment? I have searched and people say 'use open xml' ... but (apart from the fact I have no idea how to do it) would this allow me to create and save a Word document that will be able to be opened by people using old copies of Word (say as far back as Word 97).

And I keep reading the words 'Sharepoint Server' but, again, have no idea what/how this might fit in with a .net web application.

Can anyone point me in the right direction please?

2
  • Do you know why it doesn't work on your server? Commented Jan 10, 2011 at 11:44
  • What kind of file format do you want to generate? .doc, .docx, .rtf, ... Commented Jan 10, 2011 at 11:46

5 Answers 5

3

OpenXML is the right way to go right now. Creating a Word document from scratch is actually fairly easy and there are many tutorials.

Just because you tagged your question with Sharepoint I am not sure whether you are using Sharepoint - if so there is even some example code about Generating Documents from SharePoint Using Open XML Content Controls directly from Microsoft.

Sign up to request clarification or add additional context in comments.

5 Comments

To Rewinder: Automation doesn't work on the server because (I guess) of endless permissions issues. Microsoft say 'don't try Office Automation on the server because Office wasn't designed for that' ... support.microsoft.com/?id=257757
To OxA3: I need to generate a Word document that will be compatible with Word back to Word 97-2003
To moontear: Do I have to go down the route of XML? I thought Sharepoint allowed people to share office documents on a server. So I was hoping that I'd just be able to open a Word document, modify some of the text within it (bookmarks) and then save it where I want it.
Your requirements are very vague... Word 2007 introduced "Content controls" (old form fields). You could now insert content controls within your word document and have columns with the same name in a document library in Sharepoint - all values you enter there will get propagated to your Word documents. Besides that you can always open a Word document, modify it and save it somewhere else. XML is the most versatile solution.
I'll try to make the requirement more specific. A user is on a web page and makes selections to record that Fred Smith has agreed to attend meetings of a certain committee on 1st June and 25th July. When they click save the page needs to send an email to Fred Smith with an attachment. The attachment is a letterhead created in Word so, as well as containing logos, address etc. it also has bookmarks for the person's name and meeting dates. So I need to open the Word document, populate the bookmarks, save the document in a specific directory and attach it to the email.
1

The most likely reason the code worked on your development machine but not on the server is because your development machine probably has the Office/Word Primary Interop Assemblies installed on it but you do not say whether these are installed on the server or not. I am assuming they are not.

If your users are on Microsoft Office XP (2002) or Office 2003 AND have the Microsoft Office Compatibility Pack installed, then your best option is to use the Open XML SDK to generate a .docx file on the fly, which your users will be able to open by having the compatibility pack installed. Once your users have opened the document, they can then save it in the normal .doc format.

If your user base includes users of Office 2000 or earlier, you are out of luck as there is no compatibility pack for these versions of Office.

You will not be able to generate the .doc format on the web server without having the Office assemblies installed and, as others have pointed out, this is strongly discouraged by Microsoft.

2 Comments

The document I send out as an attachment must be a Word document compatible back to 97.
There's a difference between compatibility and the version of Word your users have installed. Generally speaking, a Word document created from version 2003 with a .doc extension will be backwards compatible to Word 97, although any content in the document dependent on features in later versions will not be available in previous versions. I suggest you use the Open XML SDK to generate a .docx document which, providing your users have Word XP or Word 2003, can use the Office 2007 compatibility pack to open the document, and then if they need to, save it in the older .doc format.
1

Have you considered using RTF? RTF files are just text, so you could use normal .Net file I/O to read in the file, change the bit you are interested in, and then write it out again. RTFs can be read by versions of Word from 97 onwards (and probably older versions too). One drawback is if you need to do any complicated etc changes to the document, which may be difficult if you are just processing the RTF as text.

1 Comment

Will RTF handle what is effectively a letterhead? With logos and other graphics in header and footer etc. and with bookmarks so I can replace specific text dynamically?
0

Others have commented on why the word automation probably isn't working and why its a no no anyway.

OpenXML is good but only supports the newer file formats.

If you want to support the older .doc file formats (Word97 etc) then you're going to have to look at another component and probably a commercial one such as (no connection) Aspose Words

1 Comment

So I can't access a Word 97 document on a Sharepoint Server, modify it and save it? I can't find any code that shows how to open a document on a sharepoint server from a .Net form.
0

Don't ever use Office Automation from a service. Office Automation was designed to automate the Office desktop applications. It assumes the presence of a UI.

It also assumes single threading. A server (especially a web server of any kind) will be operating on multiple threads, and the Office Automation code just doesn't handle that at all.

Additionally, you must have a valid Office license for every end user who uses the code that calls Office Automation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.