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);
}