none
Trying to open word document in a view in ASP.NET MVC by converting it to htm format RRS feed

  • Question

  • Hello

    I am trying to build an application using ASP.NET MVC in which I have to view a word doc in a view. I searched a lot on the web and found that Microsoft.Office.Interop.Office can be used for this. The following are ( I guess ) the most important lines in the present code...


    Application objWord = new Application();

    objWord.Documents.Open(ref sourceFilePath, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

    objWord.Visible = false;

    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;

    oDoc.SaveAs(ref savefilename, ref fltDocFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

    I am trying to convert the doc file to htm format first, after which I will use iframe to view the document in the view.

    But when I debug, the following exception is thrown at the "Application objWord = new Application()" line...

    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)).

     


    Can some one please help me.

    Thanks.

    (I have word starter 2010 installed on my machine. If that is the reason for the exception, kindly let me know)

    Monday, March 14, 2011 9:24 PM

All replies

  • Hi,

    According to your issue, you can try Microsoft.Office.Interop.Word.Application objWord =new Microsoft.Office.Interop.Word.Application ();

    I have tested the follow code and it worked fine as Windows Forms Application, the setup of machine: Word 2010 and Visual Studio 2010. In the application, I dragged a button to Form and wrote the code into button1_Click event.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Word.Application newApp = new Word.Application();
                object Source = "c:\\abc\\Source.docx";
                object Target = "c:\\abc\\Target.html";
                object Unknown = Type.Missing;
                newApp.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 = Word.WdSaveFormat.wdFormatRTF;
                newApp.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);
            }
        }
    }
    If you want to get more information about convert .docx file into other file, please see:

    http://www.codeproject.com/KB/cs/convertdocintootherformat.aspx


    SeeSeaIng
    Tuesday, March 15, 2011 9:12 AM
  • I tried to use the above mentioned code, but am still getting the following 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)).

     

    On the line : Microsoft.Office.Interop.Word.Application objWord = new Microsoft.Office.Interop.Word.Application();

     

    Is the exception related to the fact that I don't have Office 2010 installed, but have only Word Starter 2010 installed ?

     

    Sunday, May 1, 2011 6:20 PM
  • Hi Amit

    As you're not using the VSTO technology I'm going to move your question to the Word for Developers forum, where it's more on-target.

    And yes, I believe the problem is the Word Starter edition of the product. As I recall, that product does not have the programming APIs....


    Cindy Meister, VSTO/Word MVP
    Monday, May 2, 2011 5:55 AM
    Moderator