Click here to Skip to main content
15,913,467 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am using VS13 and sql server.i want to preview a word file when it upload in the database and download from the detabase.i have already use some code but there are some error.
code and error given below---i have use Technical Refence from http://www.aspsnippets.com/Articles/Display-Word-document-on-web-page-in-ASP.Net.aspx[^]

SETP 1: Added reference of Microsoft Word 11.0 Object Library
STEP 2:
C#
using Microsoft.Office;
using Microsoft.Office.Interop.Word;
using System.IO;

STEP 3:
C#
protected void Upload(object sender, EventArgs e)
{
    object missingType = Type.Missing;
    object readOnly = true;
    object isVisible = false;
    object documentFormat = 8;
    string randomName = DateTime.Now.Ticks.ToString();
    object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
    string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";
 
    //Upload the word document and save to Temp folder
    FileUpload1.SaveAs(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
    object fileName = FileUpload1.PostedFile.FileName;
 
    //Open the word document in background
    ApplicationClass applicationclass = new ApplicationClass();
    applicationclass.Documents.Open(ref fileName,
                                    ref readOnly,
                                    ref missingType, ref missingType, ref missingType,
                                    ref missingType, ref missingType, ref missingType,
                                    ref missingType, ref missingType, ref isVisible,
                                    ref missingType, ref missingType, ref missingType,
                                    ref missingType, ref missingType);
    applicationclass.Visible = false;
    Document document = applicationclass.ActiveDocument;
 
    //Save the word document as HTML file
    document.SaveAs(ref htmlFilePath, ref documentFormat, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType);
       
    //Close the word document
    document.Close(ref missingType, ref missingType, ref missingType);
 
    //Delete the Uploaded Word File
    File.Delete(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
 
    //Read the Html File as Byte Array and Display it on browser
    byte[] bytes;
    using (FileStream fs = new FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.Read))
    {
        BinaryReader reader = new BinaryReader(fs);
        bytes = reader.ReadBytes((int)fs.Length);
        fs.Close();
    }
    Response.BinaryWrite(bytes);
    Response.Flush();
 
    //Delete the Html File
    File.Delete(htmlFilePath.ToString());
    foreach (string file in Directory.GetFiles(directoryPath))
    {
        File.Delete(file);
    }
    Directory.Delete(directoryPath);
    Response.End();
}





ERROR SHOWING::
1.The type 'Microsoft.Office.Interop.Word.ApplicationClass' has no constructors defined
2.'Microsoft.Office.Interop.Word.ApplicationClass' does not contain a definition for 'Documents' and no extension method 'Documents' accepting a first argument of type 'Microsoft.Office.Interop.Word.ApplicationClass' could be found (are you missing a using directive or an assembly reference?)
3.'Microsoft.Office.Interop.Word.ApplicationClass' does not contain a definition for 'Documents' and no extension method 'Documents' accepting a first argument of type 'Microsoft.Office.Interop.Word.ApplicationClass' could be found (are you missing a using directive or an assembly reference?)
5.Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead.
6.Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead.

Please tell me whats wrong????
Posted
Comments
Philippe Mori 13-Jun-15 15:41pm    
The whole idea does not make much sense as once the file is uploaded, both the original Word docuement and the generated HTML documents are deleted.

It make no sense to send files to a server for a one time conversion.

Please read the Microsoft explanation why Office interop is not recommended in server-side settings:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

Instead, you can use Microsoft Open XML SDK. Please see my past answered referenced in this one: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA
 
Share this answer
 
Comments
Philippe Mori 13-Jun-15 15:41pm    
You are right about the fact that Office interop is not recommanded. However, Open XML SDK won't help much to save data in HTML format if this is what the user want.

Anyway the whole idea does not make much sense... See my comment to the question.
Sergey Alexandrovich Kryukov 13-Jun-15 19:43pm    
At least it will help to read the document. Generation of HTML is a separate task, even the development of mapping the data to HTML, because concepts of these different kinds of documents are very different and don't provide one-to-one correspondence.

Let's say, I just addressed to title of the question: how to "preview" a Word document, not the rest of the inquirer's post.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900