Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am editing a word document in the browser itself. Every time I press the save button it will generate a duplicate copy. How can I avoid this duplicate copy in asp.net?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop;
using Microsoft.Office;
public partial class MSWord_Display : System.Web.UI.Page
{
    string strPath = @"D:\sss\Tree\Tree\Tree\sand1.doc";
    //name= @"C:\Users\v-brahma\Downloads\Tree\Tree\sandhya_1.doc";
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Request.Params.Count > 0)
        //{
        //IF(Request["word"]!=null)
        //{
        //if (Convert.ToString(Request["word"]).Length>0)
        //{//name = Convert.ToString(Request["word"]);
        if (!IsPostBack)
        {
            // Application  Class
            ApplicationClass oWordApp = new ApplicationClass();
            object fileName = strPath;
            object readOnly = false;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;
            try
            {
                Document oWordDoc = oWordApp.Documents.Open(
                                            ref fileName,
                                            ref missing, ref readOnly,
                                            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);
                oWordDoc.Activate();
                //oWordDoc.LockServerFile(
                txtword.Text = oWordDoc.Content.Text;
                oWordDoc.Save();
                oWordDoc.Close(ref missing, ref missing, ref missing);
            }
            catch (Exception ex)
            {
                divMsword.InnerHtml = "OOPs  <b style='color:red;'>Error Encountered Error while Reading The document" + ex.Message + "  </b>";
            }
            //}

            //else
            //{
            //    divMsword.InnerHtml = "<b> Document name is missing: </b>";
            //}
        }
    }
    protected void save_Click(object sender, EventArgs e)
    {
        ApplicationClass oWordApp = new ApplicationClass();
        object fileName = strPath;
        object readOnly = false;
        object isVisible = true;
        object missing = System.Reflection.Missing.Value;
        try
        {
            Document oWordDoc = oWordApp.Documents.Open(
                                        ref fileName,
                                        ref missing, ref readOnly,
                                        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);
            oWordDoc.Activate();
            oWordDoc.Content.InsertAfter(txtword.Text);
            oWordDoc.Save();
            oWordDoc.Close(ref missing, ref missing, ref missing);

            divMsword.InnerHtml = "<b>Saved Sucess Fully </b> ";
        }
        catch(Exception ex)
        {
            divMsword.InnerHtml = "OOPs while saving <b style='color:red;'>Error Encountered Error while Reading The document" + ex.Message + " while saving </b>";
        }
    }

}


[edit]Code block, subject moved into question body - OriginalGriff[/edit]
Posted
Updated 14-Jun-11 0:12am
v3
Comments
Slacker007 14-Jun-11 6:13am    
Edited for readability.

You are trying to open your word file in ReadOnly mode on save button click
why so ?
 
Share this answer
 
Comments
Tarun.K.S 14-Jun-11 9:55am    
ReadOnly is set as false.
Hi,

you can easily C# edit Word file without generating a duplicate copy and ASP.NET export to Word with this C# / VB.NET Word library.

This library doesn't have any dependency on Microsoft Word and doesn't use C# Word Automation which makes it ideal for ASP.NET server applications.

Here is a sample C# code:
C#
// Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

// Create and save an input document. 
// You don't have to do this if you already have an input document.
// This code is only provided as a reference how input document should look like.
var document = new DocumentModel();
document.Sections.Add(new Section(document, new Paragraph(document)));
document.Save(Path.Combine(Request.PhysicalApplicationPath, "Document.docx"), SaveOptions.DocxDefault);
// Instead use:
// Load an input document from application's root directory.
// var document = DocumentModel.Load(Path.Combine(Request.PhysicalApplicationPath, "Document.docx"), LoadOptions.DocxDefault);

// Edit first Run in first Paragraph or add new Run if Paragraph doesn't contain Run as first child.
var paragraph = document.Sections[0].Blocks.Cast<paragraph>(0);
if (paragraph.Inlines.Count > 0 && paragraph.Inlines[0].ElementType == ElementType.Run)
	paragraph.Inlines.Cast<run>(0).Text = txtword.Text;
else
	paragraph.Inlines.Add(new Run(document, txtword.Text));

// Microsoft Packaging API cannot write directly to Response.OutputStream.
// Therefore we use temporary MemoryStream.
using (MemoryStream documentStream = new MemoryStream())
{
	document.Save(documentStream, SaveOptions.DocxDefault);

	// Stream file to browser.
	Response.Clear();
	Response.ContentType = "application/vnd.openxmlformats";
	Response.AddHeader("Content-Disposition", "attachment; filename=Document.docx");

	documentStream.WriteTo(Response.OutputStream);

	Response.End();
}
 
Share this answer
 

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