Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7536335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:32:47+00:00 2026-05-30T06:32:47+00:00

I have a script that creates files on multiple threads. Occassionally, I will get

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:32:48+00:00Added an answer on May 30, 2026 at 6:32 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, we have a script that creates batch files to XCOPY our
I have a PHP script that creates other PHP files based on user input.
I have written a CGI script that creates an image dynamically using GET data.
I have a large SQL script that creates my database (multiple tables, triggers, and
Hi I have a script that creates a file and stores it on the
I have a bash script that creates a Subversion patch file for the current
I have a Python script that reads through a text csv file and creates
I have created a script that saves some files in the Documents directory of
I have php script that creates a temporary watermark image for users that are
Say I have a PHP script that creates a cookie that expires 10 days

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.