Azeem Tariq
Azeem Tariq

Reputation: 7

Word.Application.Quit() Function is not Working

I am developing a application in which i have to convert Word (.doc) file to Text File , here is Code Example:

//Creating the instance of Word Application
Word.Application newApp = new Word.Application();

// specifying the Source & Target file names
object Source = "F:\\wordDoc\\wordDoc\\bin\\Debug\\word.docx";
object Target = "F:\\wordDoc\\wordDoc\\bin\\Debug\\temp.txt";
object readOnly = true;
// Use for the parameter whose type are not known or  
// say Missing
object Unknown = Type.Missing;

// Source document open here
// Additional Parameters are not known so that are  
// set as a missing type;
newApp.Documents.Open(ref Source, ref Unknown,
     ref readOnly, ref Unknown, ref Unknown,
     ref Unknown, ref Unknown, ref Unknown,
     ref Unknown, ref Unknown, ref Unknown,
     ref Unknown, ref Unknown, ref Unknown, ref Unknown);

// Specifying the format in which you want the output file 
object format = Word.WdSaveFormat.wdFormatDOSText;
object orgfrmat = Word.WdSaveFormat.wdFormatFilteredHTML;
//Changing the format of the document
newApp.ActiveDocument.SaveAs(ref Target, ref format,
        ref Unknown, ref Unknown, ref Unknown,
        ref Unknown, ref Unknown, ref Unknown,
        ref Unknown, ref Unknown, ref Unknown,
        ref Unknown, ref Unknown, ref Unknown,
        ref Unknown, ref Unknown);

// for closing the application
object saveChanges = Word.WdSaveOptions.wdSaveChanges;
newApp.Quit(ref saveChanges, ref Unknown, ref Unknown);

but my application is not closing properly , when i try to read content of temp.txt file using this code

using (StreamReader sr = new StreamReader("F:\\wordDoc\\wordDoc\\bin\\Debug\\temp.txt"))
{
    rtbText.Text = sr.ReadToEnd();
    // Console.WriteLine(line);
}

it throws this exception

The process cannot access the file 'F:\wordDoc\wordDoc\bin\Debug\temp.txt' because it is being used by another process.

Can anyone tell me how to fix it?

Upvotes: 0

Views: 3266

Answers (1)

steve16351
steve16351

Reputation: 5812

Try using Marshal.ReleaseComObject to clean up the COM object before trying to open the file.

For example.

object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
newApp.Quit(ref saveChanges, ref Unknown, ref Unknown);

Marshal.ReleaseComObject(newApp);

using (StreamReader sr = new StreamReader((string)Target))
{
    Console.WriteLine(sr.ReadToEnd());
}

Alternatively, to avoid the use of COM (and needing Office installed) you could use a 3rd party library. I've no experience with this library http://docx.codeplex.com/ , but for a simple test it seems to do the job. If you document has complex formatting it may not work for you.

string source = @"d:\test.docx";
string target = @"d:\test.txt";

// load the docx
using (DocX document = DocX.Load(source))
{
    string text = document.Text;

    // optionally, write as a text file
    using (StreamWriter writer = new StreamWriter(target)) 
    {
       writer.Write(text);           
    }

    Console.WriteLine(text);
}

Upvotes: 4

Related Questions