4

I am trying to convert .doc file to .htm format to view in an ASP.NET MVC page.

I am using the following code in C# :

using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;

....

Microsoft.Office.Interop.Word.Application objWord = new Microsoft.Office.Interop.Word.Application();

            object source = @"C:\Users\XYZ\Desktop\ScreenShot.doc";
            object target = @"C:\Users\XYZ\Desktop\ScreenShot.html";
            object unknown = Type.Missing;
            objWord.Documents.Open(ref source, ref unknown,
                 ref unknown, ref unknown, ref unknown,
                 ref unknown, ref unknown, ref unknown,
                 ref unknown, ref unknown, ref unknown,
                 ref unknown, ref unknown, ref unknown, ref unknown);

            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;
            objWord.ActiveDocument.SaveAs(ref target, ref format,
                    ref unknown, ref unknown, ref unknown,
                    ref unknown, ref unknown, ref unknown,
                    ref unknown, ref unknown, ref unknown,
                    ref unknown, ref unknown, ref unknown,
                    ref unknown, ref unknown);

I have tried to google the way to convert .doc ( even .ppt ) to .htm format and have always found code somewhat similar to the above.

But I keep getting this exception :

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

at the line :

Microsoft.Office.Interop.Word.Application objWord = new Microsoft.Office.Interop.Word.Application();

Is this due to the reason that I have a Word Starter 2010 installed and not the complete Office 2010, or is there some other solution to it ?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Jake
  • 16,329
  • 50
  • 126
  • 202
  • 5
    Do not automate Office on a server. – SLaks May 01 '11 at 18:46
  • @SLaks : Can you please explain...thanks. – Jake May 01 '11 at 18:47
  • 2
    http://support.microsoft.com/kb/257757 – SLaks May 01 '11 at 18:53
  • @SLacks : I even tried this code on a simple C# console application; I still have the exception. – Jake May 01 '11 at 18:54
  • Somewhat related: http://stackoverflow.com/questions/3967191/library-to-convert-word-document-text-to-html – robi-y May 01 '11 at 19:03
  • @SLacks: The project that I am working on allows me to use only .NET features, is there some other solution? Is it for sure that this problem is not related to the version of Microsoft Office installed on the machine ? – Jake May 01 '11 at 19:09

2 Answers2

2

Using COM objects from MS Office on server side is not good idea. The frist problem is technical - there are several pitfalls with processes (i.e. sometimes excel/word does not quit after calling Quit()). It is not easy, but it is solvable.

However the second problem is licensing. You need license for every user who will be using the MS Office. So, if you want use it on internet web, you will have serious financial issues.

There are several libraries which can open (save, convert, etc...) MS Office formats without having MS Office installed. I worked once with Aspose library, but there are several others.

TcKs
  • 25,849
  • 11
  • 66
  • 104
1

You got this exception as COM object is not configured to allow launch and access permissions for the aspnet user identity. It's better to change Application Pool Identity user to be "Network Service", which which has enough permissions to execute COM+ components.

For more details check this

Ahmed Atia
  • 17,848
  • 25
  • 91
  • 133
  • Can you please explain how the application pool identity is changed? – Jake May 01 '11 at 19:00
  • From Run --> type inetmgr, this will open IIS Manager, select application pool which is attached to your website, then from Actions menu, select Advanced Settings, then change user using Identity option. – Ahmed Atia May 01 '11 at 19:05