SaveAs exception "Word cannot save this file because it is alreadyopen elsewhere"

R

rsotolongo

Hello,

I really hope that somebody can helps me. Look that code that I have:

private void WriteDOC(string FileName, List<string> Strings)
{
Microsoft.Office.Interop.Word.ApplicationClass aApp = new
Microsoft.Office.Interop.Word.ApplicationClass();
object missing = System.Reflection.Missing.Value;
object filename = FileName;
object noflag = false;
Microsoft.Office.Interop.Word.Document aDoc = aApp.Documents.Add(ref
missing, ref missing, ref missing, ref noflag);
aDoc.Activate();
for (int i = 0; i < Strings.Count; i++)
{
aApp.Selection.TypeText(Strings);
aApp.Selection.TypeParagraph();
}
aDoc.SaveAs(ref filename, 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, ref missing);
aDoc.Close(ref missing, ref missing, ref missing);
aDoc = null;
aApp.Quit(ref missing, ref missing, ref missing);
aApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}

I make N calls to that method:

WriteDOC("c:\\temp\\f1.doc", strings1)
WriteDOC("c:\\temp\\f2.doc", strings2)
..
..
..

The first call it's OK. Generates the file "f1.doc" with the strings
of the list 1, but when it tries to save the second time, trows the
exception "Word cannot save this file because it is already open
elsewhere". I couldn't find an answer. Please help me.
 
S

surfbored

I think your problem comes from using SaveAs twice. It works the first time
because the file does not yet exist/is not open, the second time fails
because the document now exists and is open. The second call must be "Save"
not "SaveAs".

SB
 
S

surfbored

Sorry, I just re-read your post. My answer does not seem to apply after
rereading your code -- although the error message is one that I have seen
many times for the reason I posted previously.

SB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top