1

I am processing a word document using mergefield and bookmark.The application runs well while in debug mode but it throws error on release mode.I am new to word automation.Please help.If need any other details please let me know.Source code is given below.

> Blockquote
 object nothing = System.Reflection.Missing.Value;
        object filename = "E:\\Templatenew.doc";
        object destination = "E:\\new.doc";
        object nottrue = false; 

        object oPrintdate = "printdate";
        object oClientname = "clientname";
        object oClientsname = "clientsname";
        object oAsondate = "asondate";
        object oAmount = "amount";



        myWordApp.Visible = false;
        myWordDoc = myWordApp.Documents.Add(ref filename, ref nothing, ref nothing, ref nothing);

        myWordDoc.Activate();

        myWordDoc.Bookmarks.get_Item(ref oPrintdate).Range.Text = System.DateTime.Now.ToShortDateString();
        myWordDoc.Bookmarks.get_Item(ref oClientname).Range.Text = objproperty.firstname +"" + objproperty.lastname;

        myWordDoc.Bookmarks.get_Item(ref oAmount).Range.Text = "10000";



        myWordDoc.SaveAs(ref destination, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing,
                            ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
        myWordDoc.Close(ref destination, ref filename, ref nothing);



        Response.ContentType = "application/msword";
        myWordApp.Application.Quit(ref nottrue, ref nothing, ref nothing);

        Response.WriteFile("E:\\new.doc");
        Response.End();

> Blockquote
Keith
  • 20,636
  • 11
  • 84
  • 125
shakz
  • 629
  • 3
  • 14
  • 38
  • Do not automate Word on a server. It will result in severe problems, including this one. – SLaks Jul 27 '11 at 14:23
  • What is the exact error message and what line is it thrown from? – Keith Jul 27 '11 at 14:23
  • Is it possible that new.doc is already open by another process? Check to see if winword.exe processes are still running. – Keith Jul 27 '11 at 14:24
  • "Word cannot save this file because it is already open elsewhere." This is the error message – shakz Jul 27 '11 at 14:39

1 Answers1

0

You are not properly disposing of the Word objects. This will prevent the automated Word process from exiting -- if you open Task Manager you should see winword.exe processes that are still running. It is likely that at least one of these processes has a lock on your new.doc file.

Check out the links below to learn how to properly reference and dispose of your Word objects. In short:

  • Never use 2 dots with COM objects -- always create a separate variable reference to any object property you reference. (For example, instead of myWordApp.Documents.Add... you should do var documents = myWordApp.Documents; documents.Add...)
  • Where possible, call Close/Quit methods on all COM objects that you reference when you are done with them. (I think you are already doing this.)
  • Properly dispose of all COM objects referenced by setting each reference to null and invoking the proper garbage collection methods. (See the 2nd link)

How do I properly clean up Excel interop objects? (This talks about Excel automation but still applies to Word.)

http://www.xtremevbtalk.com/showthread.php?p=1326018

After you run your application make sure there are no winword.exe processes left hanging in Task Manager.

(As a side note, you have to be really careful using Word automation. A lot of people on stackoverflow will simply tell you to never do it. In my experience, you can do it as long as you adhere to the rules stated above and keep the Word process open as short as possible. I've been using it on a moderately trafficked site for over a year now without a single problem.)

Community
  • 1
  • 1
Keith
  • 20,636
  • 11
  • 84
  • 125