Search code examples
c#multithreadingcomms-word

Document file won't save properly


I have a script that creates files on multiple threads. Occassionally, I will get the following error on the document.SaveAs() call:

{"Word cannot save or create this file. Make sure that the disk you want to save the file on is not full, write-protected, or damaged.\r (C:\...\fileName.docx)"}

This is maddening because it seems to happen sometimes and not others. I've been spending the last day trying to figure it out, and have made very little progress. One thing I have ruled out is the possibility of more than one thread trying to create the same file. I am certain this isn't happening. Is there anything I'm missing in my code that would be causing this, or is this just a fact of life when working with com objects? I'm doing exactly what code tutorials out there are saying, maybe I should abandon using multiple threads?

BuildDocumentsThread(){
        var word = new Microsoft.Office.Interop.Word.Application();
        word.Options.CreateBackup = false;
        var wordQuit = (Microsoft.Office.Interop.Word._Application)word;

        foreach(var value in values){
            FormBuilder.BuildSummaryForm(word, value);
        }

        wordQuit.Quit();
}

public static void BuildSummaryForm(Application word, string value) {
        var summaryFormPath = Utilities.GetSummaryFilePath(value);

        if (File.Exists(summaryFormPath))
            File.Delete(summaryFormPath);

        object path = summaryFormPath;
        object readOnly = false;
        object o = System.Reflection.Missing.Value;

        var document = word.Documents.Add(ref o, ref o, ref o, ref o);
        document.Activate();

        Paragraph p1 = document.Content.Paragraphs.Add(ref o);
        p1.Range.Font.Name = "Arial";
        p1.Range.Font.Size = 10;
        p1.Range.Text = value;

        document.SaveAs(ref path, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
        document.Close();
}

Solution

  • If the SaveAs is not thread safe as describe by John Koerner in the comment, you could try to lock the SaveAs and Close.

    In this case I'm mot sure the multithreading process will be usefull anymore, but it's worst a try. At least you will know if your error is coming from the SaveAs function.


    To add some infromation following your comment:

    add a class variable to create the lock

    private static object _savelock = new object();
    

    Then in you worker:

    lock(_savelock)
    {
       document.SaveAs(ref path, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
    }
    document.Close();
    

    In this case, no save operation will be done at the same time. Locking the close as well may be necessary

    • 1 thread take the lock and do the save
    • In the meantime 2 thread try to do a save but wait until the previous thread finish the save and release the lock

    In this case you don't really care if it's the same instance of word or not. in both case it will work.

    The main drawback is that if the save operation take 95% of the time of the thread, multi threading become nearly useless because by locking the save operation, only 5% of the process is really multi threaded. However if "freeing" the ui and let the user continue working while the process is running is the aim, it's a viable solution.

    Check http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx for the lock pricinple