1

I've looked at a few sources but they seem to have missed the mark in terms of solving the problem.

Error: System.Runtime.InteropServices.COMException'occurred in PFML_Auto_Fax.exe Additional information: This file is read-only.

Code:

public void Save()
{
    oDoc.Saved = true;
    oDoc.SaveAs2(Path.Combine(Environment.CurrentDirectory, "temp.doc"));// this is where 
    ((_Document)oDoc).Close();
    ((_Application)oWord).Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
    oDoc = null;
    oWord = null;
    GC.Collect();
}

Note: It loops and saves once and the 2nd loop will be unable to save at line indicated above!

As you can see I set the document and application to null and then forced a garbage collection but the program still insists that it is open.

The thing is my problem loops creating and saving this particular file over and over. But if it actually exits when I wanted it to I would not have a problem.

Main problem: Dispose exit and leave my newly saved file alone so I can overwrite it in my next loop and use it.

Similar questions:

InteropServices.COMException (This file is read-only).

Disposing of Microsoft.Office.Interop.Word.Application

EDIT: Here is the loop in question

foreach (Principal p in principalList)
{
    WordManager wManager = new WordManager(wordDocDir);
    wManager.ReplaceText("XX", DateTime.Now.ToString("dd"));
    wManager.ReplaceText("INSERTNAME1", String.Format("{0} {1} {2}", p.Salutation != null ? p.Salutation.Trim() : "Principal", p.FristName != null ? p.FristName : "", p.LastName !=null ? p.LastName : ""));

    foreach (string key in toReplace.Keys)
    {
        string outkey;
        toReplace.TryGetValue(key, out outkey);
        wManager.ReplaceText(key, p.AdditionalText[Int32.Parse(outkey)]);
    }
    wManager.Save();
    wManager = null;
    GC.Collect();
    fManager.SendFax(p);
}
Community
  • 1
  • 1
Ya Wang
  • 1,758
  • 1
  • 19
  • 41
  • what is the line `fManager.SendFax(p);`? I guess it opens the document and keeps it open (read-only) – kennyzx Sep 08 '15 at 16:28
  • You really want to use [Docx](https://docx.codeplex.com/) for a task as simple as that. It's done in [three lines](http://cathalscorner.blogspot.de/2009/02/docx-net-library-for-manipulating-word.html) – Thomas Weller Sep 08 '15 at 17:19
  • The linked answer uses `ReleaseComObject()` 3 times. You use it 2 times only. – Thomas Weller Sep 08 '15 at 17:22
  • @ThomasWeller He really should not be calling ReleaseComObject at all. See [this post by Hans Passant](http://stackoverflow.com/a/17131389/80274) for much more detail. – Scott Chamberlain Oct 06 '15 at 16:58
  • @ScottChamberlain: I wondered about that as well, since in one of my projects, `ReleaseComObject` caused trouble. Thanks for the link. Now I have a good reference – Thomas Weller Oct 06 '15 at 17:09

1 Answers1

0

Seems like DocX interlop was better. The regular office interlop has trouble closing word.

Ya Wang
  • 1,758
  • 1
  • 19
  • 41