locked
Open/Edit a word document on a web page RRS feed

  • Question

  • User294477991 posted

    HI,

    Iam trying to open/Edit a word document for changes as below on a web page(default.aspx)
    Its works in visual studios but not on the web page.
    Can anyone help me get this resolved.

    Below is the code iam using to open the word document:


    protected void Button1_Click(object sender, EventArgs e)
        {

           
    Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

            object file = "C:\\temp1\\poForM.doc";

            object objFalse = false;
            object objTrue = true;
            object missing = System.Reflection.Missing.Value;
            object emptyData = string.Empty;
            wordApp.Visible = true;
            Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref file, ref objFalse, ref objFalse,
                    ref objFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue,
                    ref missing, ref missing, ref missing);
            aDoc.Activate();
    }

    The document is never shown/opened.

     

    Thanks in advance.

    Tuesday, April 20, 2010 12:36 PM

Answers

  • User-1199946673 posted

    Prompt user to open the required file,edit the word document,save the document
     

    I already gave you a link how to do this

    and than copy the latest or the saved one to the server,so all the other process is done

    You cannot automate this. It is up to the user if they want to upload the saved document

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 21, 2010 6:02 PM

All replies

  • User-1199946673 posted

    Its works in visual studios but not on the web page.
     

    You need to remember that ASP.NET runs on server side. In your development environment, when you're using Visual Studio, your PC is used as the server. But in a production environment, what you're doing here is opening a Word document on the web server!

    Also, Microsoft.Office.Interop should not be used in a web environment!

    "Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment."

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2

    Especially when you're calling wordApp.Visible = true, I think you're server will crash very soon

    Tuesday, April 20, 2010 4:16 PM
  • User294477991 posted

    Thanks a lot hans for your reply.

    Can you or anyone please provide  me an alternative to accomplish this,with some code examples.

    Thanks in advance,

    Terry.

    Wednesday, April 21, 2010 9:38 AM
  • User-1199946673 posted

    Can you or anyone please provide  me an alternative to accomplish this,with some code examples.
     

    That depands on what you want to accomplish? Do you want to edit the word document in ASP.NET on serverside, or do you want the user to edit the document?

    Wednesday, April 21, 2010 9:58 AM
  • User294477991 posted

    I want the user to edit,make necessary changes ,save the file and use it for text selection (this on a different button).

    Thank you, 

    Wednesday, April 21, 2010 10:02 AM
  • User-1199946673 posted

    I want the user to edit,make necessary changes 
     

    That's easy!

    http://forums.asp.net/t/1547457.aspx#3791722

    But then you must realize that the user is opening the document on his local computer (Wod needs to be installed)

    save the file

    This is the problem. When the user edits the document, it's up to him to upload the file to the server

    Wednesday, April 21, 2010 11:37 AM
  • User294477991 posted

    Thanks for your reply hans.

    I already used that code ,here is my full code,the below one needs to be worked on web page.

     

    protected string uploadFile = string.Empty; 
      
        Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            uploadFile = Path.Combine(Request.ApplicationPath, @"temp1\" + Path.GetFileName(fleAttach.PostedFile.FileName));
            fleAttach.SaveAs(uploadFile);
            txtHidden.Text = uploadFile;
            OpenWord(uploadFile);
           

        }
       
        public void OpenWord(string wordFileName)
        {
            object fileName = wordFileName;
            object objFalse = false;
            object objTrue = true;
            object missing = System.Reflection.Missing.Value;
            object emptyData = string.Empty;
            WordApp.Visible = true;
            Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref objFalse, ref objFalse,
                    ref objFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue,
                    ref missing, ref missing, ref missing);
            aDoc.Activate();
          
        }

    This works in Vstudio,Opens word document,I can make changes,and than save.

    When i use the same default page as http://merge.com/default.aspx, Word document is never opened.The page does nothing.

    Can you or anyone please help me figure out this,i am strugging on this since a month.

    Thanks in advance.

    Wednesday, April 21, 2010 11:59 AM
  • User-1199946673 posted

    This works in Vstudio,Opens word document,I can make changes,and than save.

    When i use the same default page as http://merge.com/default.aspx, Word document is never opened.The page does nothing.

    I already explained this in my first reply! The document is opened on the server (provided Word is installed on the server!), on your development machine the server is your local PC so it will work, but in a web environment this will NOT work. I think you should do some more research on what Web develpment is all about! When a user will edit a word document on his local machine, is not connected whatsover with your website!

     

    Wednesday, April 21, 2010 12:02 PM
  • User294477991 posted

    Thanks for your reply hans.

    Iam lot more confused on this.What all i need to do is

    1.Browse a file using fileupload

    2.Use the file upload file and when hit on a button,the file needs to opened/edited(user will have word on his local machine/where we he uses this application).

    3.User makes necessary changes,saves the file.

    How to do this?

    For this,i downloaded the file uploaded file to the server,saved in a temp location,so user needs to open that temp file,edited,and saved.

    Can you please help or point me in the right direction if iam doing wrong.

     Thanks so much for your help.

    Wednesday, April 21, 2010 12:23 PM
  • User-1199946673 posted

    I understand what you want, but you don't seem to understand that what you want is not possible in a web environment!

    1.  That's no problem
    2. This is were the problem start. A document that is saved on the webserver, canot be edited by a user that's visiting a website. When they want to edit the document, it first need to be downloaded to their local computer, where it can be edited, but this is a copy of he document that is stored on the webserver.
    3. So when it is edited localy, it should be copied back to the web server 

    Wednesday, April 21, 2010 12:38 PM
  • User294477991 posted

    Thank you so much hans for your time in replying.

    Now i understand that Server files cannot be edited from the websites.

    Is there any other way,i can do,for example,

    Prompt user to open the required file,edit the word document,save the document and than copy the latest or the saved one to the server,so all the other process is done.
    Can you please provide some samples for this or else i will close this ,if this cannot be done at all,in any ways.

    Thank you

    Wednesday, April 21, 2010 1:03 PM
  • User-1199946673 posted

    Prompt user to open the required file,edit the word document,save the document
     

    I already gave you a link how to do this

    and than copy the latest or the saved one to the server,so all the other process is done

    You cannot automate this. It is up to the user if they want to upload the saved document

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 21, 2010 6:02 PM
  • User1646115635 posted

    Its possible . You can achieve this ussing third party tool like Text control ( http://www.textcontrol.com).

    Friday, December 9, 2011 2:11 AM
  • User-1199946673 posted

    Its possible . You can achieve this ussing third party tool like Text control ( http://www.textcontrol.com).

    What is possible? You're showing a link to a third party tool, which allows you to edit (among other things) to edit a Word document on the server. But you can't do what is asked here....

    Friday, December 9, 2011 2:55 AM
  • Friday, December 9, 2011 4:35 AM
  • User669302094 posted

    Hi,

    I have done one example for this , hope this will help you.

    follow  my article in this link-

    http://steptodotnet.blogspot.in/2012/11/how-to-edit-word-document-using-aspnet.html

    Try this and let me know.

    Friday, November 2, 2012 2:33 AM
  • User-841541180 posted

    I know this is a bit late, but there is a *real* solution to this problem, although its far from perfect.

    Step 1) The file to be edited must be available on the web server and able to be accessed using the WebDav protocol. If you deal with *real* documents this is easy, in our case where we store our documents in the database we make a local temp copy.

    Step 2) And this only works in IE, you can fire up MS Word using an Active X control which allows MS Word to access a document over WebDav for edit.

    <body onload="startWord('https://bastest.balen.co.uk/Documents/0AABCA9E-88FA-479A-8B98-09DC1DD606EC.doc')">
    <a href="javascript:startWord('https://bastest.balen.co.uk/Documents/0AABCA9E-88FA-479A-8B98-09DC1DD606EC.doc')">Open Generated File</a>
    <script type="text/javascript" language="JavaScript">
    function startWord(strFile)
    {
    var myApp = new ActiveXObject("Word.Application");
    if (myApp != null)
    {
    myApp.Visible = true;
    myApp.Documents.Open(strFile);
    }
    }
    </script>
    </body>

    Step 3) [If using temp files only] Write a small server side server (could be a thread that is kicked off in application_start) which monitors the MS Word files for changes, when the user saves the document the monitor picks it up and save them back to the database or moves the file or whatever.

    As I said, not perfect, but works reasonable well, so long as your users use IE.

    Friday, November 23, 2012 5:54 PM