views:

692

answers:

6

Hi,

I'm using VS 2005, asp.net 2.0.

I need to read a word document (.doc) in asp.net, make some replaces and then generate another word document.

So far OK. I've done it in a machine that have the Office 2003 installed, so in VS 2005 I've added a reference to the "Microsoft Word 11.0 Object Library". Everything worked perfectly. Code to open/save the word document: http://www.dnzone.com/go?1387

Question 1: I need to open the same project in a machine that has Office 2007 installed and make it run. When I do it, it doesn't recognizes the references.. I only have the possibility to add reference to the "Microsoft Word 12.0 Object Library". How can I make it work smoothly in both machines with the less effort possible? Is there a way to do so?

Question 2: Will this application work fine when I deploy it to a web server? Do I have to register some dll or component in the server or do anything else in order to this office integration to work?

Thank you very much!

Rodrigo

+1  A: 

You should never use the Word API from server code. It is not supported and will generally not work reliably.

John Saunders
Yeah, I've heard about that... tks!
Rodrigo
A: 

I ran into this problem a while back -- we ended up saving it as an RTF file which still comes out formatted then using FileStream and .Replace(someStringValue) based on key values, such as #DateForEventStart so you had something like this

    public void something()
    {
        StreamReader stream = new FileStream(filePath);
        string template = stream.ReadToEnd();
        template.Replace(templateKeyValue, objectValue);
    }

then you can stream that out to a memstream or whatever you need. I don't know if that's possible for what you're doing, but it's a thought.

jeriley
I don't think I can use RTF because I've small tables to insert into the word document when doing the replaces. In the Word XML there's a table structure to follow. Looking at the RTF contents, I don't see how could I accomplish that goal...
Rodrigo
That's a piece of information you left out so that changes the recommendation ;-). Using the msdn xml guidelines (fairly easy) is a a way if you're wanting to generate a doc with somewhat complicated content -- images, charts, etc and stream that out as a response.
jeriley
A: 

You might want to take a look at the Aspose products. They are designed to work in a server environment (but are not free)

Stephan Eggermont
I rather prefer to go on a free solution. But thank you!
Rodrigo
+1  A: 

Save yourself alot of work and use the Word XML (available in Word 2002 and up) to create your templates. Check the XML code generated to see where you have to make your replacements. Compatible with Office 2002, 2003, 2007, and no server component needed.

Word XML looks the same as the corresponding DOC file in 99% of the cases, yet better maintainable. Plus support for all Word features, which is why you might favor it over RTF.

Jan Jongboom
Jan, that's the way I'm doing it. But I'm facing right now misterious problems with the replaces. Somehow when there's a random combination of replaces made on the document, it becomes impossible to be opened by the Word, it gives me an error saying there're problems in the document sintax and it can't be opened. I'm still trying to figure out what's going on.Any ideas on that???
Rodrigo
Are you doing it with Word XML format? You state that you have .DOC files in your question. The Word XML format is a format provided by 2002/2003/2007 and has an extension .xml. When using the format, you'll have to be aware that you aren't breaking the XML structure, but it's quite clear; haven't had that much problems with it, as long as you are just replacing some small parts (like a templating system).
Jan Jongboom
Yes this is word xml format. To generate my XML Template I opened the .doc in word and Saved As XML format. Then I read the xml file contents and perform t he replaces. And yes, I'm just replacing small parts of the content. ie: strXmlContents = strXmlContents.Replace("##Name##", "Pamela Anderson");
Rodrigo
Can you provide a sample XML doc, and a code sample which breaks your document?
Jan Jongboom
Actually I've just found out that the problem has nothing to do with the replaces. Randomly when the .doc file is generated, some garbage pieces of word document are being generated right at the end of the file contents, that's why it is not opened by the Word. If I open the file in TextPad and clean that gargabe up, it opens! Now the mistery is... why that garbage is generated?? I'm generating the file with StreamWriter...
Rodrigo
Please create a new question for why the garbage is added. This is not your original question, and that's quite how SO works :-) Not so much discussion, but rather just about plain answers. Helps others with the same problem too.
Jan Jongboom
A: 

We've found out the problem, it was something related with the file creation. I was using FileStream with FileMode.Open on an already existing file. I changed to FileMode.Create to create a brand new file and.. voillá! :)

Thank you very much for everybody for your replies, specially to Jan Jongboom.

Cheers from Brazil!

Rodrigo
A: 

It is not supported and will generally not work reliably,so I suggest to use a c# word component Spire.Doc, I use it long time, quit good.It supports search a word and replace it .

HallAurora