locked
ERROR:Process being used another person RRS feed

  • Question

  • User-515898492 posted

    hi all,

     i Have problem in word to html process.. When i cilck button word file translate to html on the time i want show that html page in redirect response..but i have the following error.


    The process cannot access the file 'C:\Documents and Settings\Administrator\Desktop\WordToHtml\WordToHtml\test2l.doc.htm' because it is being used by another process.

    the code below

    public partial class _Default : System.Web.UI.Page
    {
        protected Microsoft.Office.Interop.Word.ApplicationClass objWord = new ApplicationClass();
        protected string strPathToUpload; //Path to upload files "Uploaded"
        protected string strPathToConvert; //Path to convert uploaded files and save
        object fltDocFormat = 10; //For filtered HTML Output
        protected object missing = System.Reflection.Missing.Value;
        protected object readOnly = false; //Open file in readOnly mode
        protected object isVisible = false;//The process has to be in invisible mode
        //protected object originalFile = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            
            if (!(fUpload.HasFile))
            {
                lblMessage.Text = "Please choose file to upload";
            }
            else
            {
                try
                {
                    //To check the file extension if it is word document or something else
                    string strFileName = fUpload.FileName;           
                    string[] strSep = fUpload.FileName.Split('.');
                    int arrLength = strSep.Length - 1;
                    string strExt = strSep[arrLength].ToString().ToUpper();
                    //Save the uploaded file to the folder
                    strPathToUpload = Server.MapPath("Uploaded");
                    //Map-path to the folder where html to be saved
                    strPathToConvert = Server.MapPath("WordToHtml");
                    object FileName = strPathToUpload + "\\" + fUpload.FileName;
                    object FileToSave = strPathToConvert + "\\" + fUpload.FileName + ".htm";
                    if (strExt.ToUpper().Equals("DOC"))
                    {
                        fUpload.SaveAs(strPathToUpload + "\\" + fUpload.FileName);
                        lblMessage.Text = "File uploaded successfully";
                        //open the file internally in word. In the method all the parameters should be passed by object reference
                        objWord.Documents.Open(ref FileName, ref readOnly,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing, ref missing);
                        //Do the background activity
                        objWord.Visible = false;
                        Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                        oDoc.SaveAs(ref FileToSave, 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);
                        lblMessage.Text = fUpload.FileName +" converted to HTML successfully";
                        fUpload.Dispose();
                    


                    }
                    else
                    {
                        lblMessage.Text = "Invalid file selected!";
                    }
                    //Close/quit word
                    objWord.Quit(ref missing, ref missing, ref missing);
                  

                    Session["call"] = strFileName.ToString();


                    Response.Redirect("~/WordToHtml/" + call + ".htm", false);

                  
                    
                }

    Thursday, May 13, 2010 8:48 AM

Answers

  • User-2119480821 posted
    if (objWord != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(objWord);
    }
    objWord = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();


     Can you explain what you actually doing by creating the doc file... what background activity you doing?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2010 2:03 AM
  • User406085191 posted

    Hi try this to kill all wordinstances

    private void closeword()
        {
    
            System.Diagnostics.Process[] wordInstances = System.Diagnostics.Process.GetProcessesByName("WINWORD");
            foreach (System.Diagnostics.Process wordInstance in wordInstances)
            {
                try
                {
                    wordInstance.Kill();
                }
               catch 
                { }
            }
    
        }
    

    and let me know wether it is working or not
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 17, 2010 5:47 AM

All replies

  • User-2119480821 posted

    You need to release all word object instance from memory.. 

    Thursday, May 13, 2010 9:54 AM
  • User-515898492 posted

    hi Suthish nair

       Can you sent release comment for that doc release..Please..

    Thank you..


    Friday, May 14, 2010 1:32 AM
  • User-2119480821 posted
    if (objWord != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(objWord);
    }
    objWord = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();


     Can you explain what you actually doing by creating the doc file... what background activity you doing?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2010 2:03 AM
  • User-515898492 posted

    hi,

       Actually when i upload a word doc it automatically convert to html page, the whole content of word doc

      should be display on the html page, here i want the converted html page must show the button click..


    Thank You,



    Friday, May 14, 2010 3:15 AM
  • User-2119480821 posted

    After completion of conversion you need to relase all word objects from memory.. as i mentioned above.. 

    Friday, May 14, 2010 3:36 AM
  • User-515898492 posted

    Hi Suthish Nair,

       hi i got the result for your valuable reply.

    thanks a lot..

    G.Arunabathan

    Monday, May 17, 2010 4:07 AM
  • User-2119480821 posted

    Mark As Answer if query is resolved ... 

    Monday, May 17, 2010 4:22 AM
  • User-515898492 posted

    hi,

      First time only its opened..next conversion its not opened..


    Monday, May 17, 2010 5:21 AM
  • User406085191 posted

    Hi try this to kill all wordinstances

    private void closeword()
        {
    
            System.Diagnostics.Process[] wordInstances = System.Diagnostics.Process.GetProcessesByName("WINWORD");
            foreach (System.Diagnostics.Process wordInstance in wordInstances)
            {
                try
                {
                    wordInstance.Kill();
                }
               catch 
                { }
            }
    
        }
    

    and let me know wether it is working or not
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 17, 2010 5:47 AM
  • User-515898492 posted

    Hi Vijaya,

                  i got the output from ur valuable code.

                        Thanks all.

    Arunabathan.G

    Friday, May 21, 2010 4:04 AM
  • User406085191 posted

    Mark As Answer if problem is solved so that it is helpful to others 

    Friday, May 21, 2010 4:46 AM