none
Word add-in intermittently throws error: 'System.IO.FileNotFoundException: Could not find file' RRS feed

  • Question

  • I have a Word addin that saves a temporary .docx file of the document in the ~\AppData\Local\Temp directory and then that file is uploaded to a server. Occasionally it will throw the exception: : 'System.IO.FileNotFoundException: Could not find file' ... 'System.ArgumentException: Parameter is not valid.', but the file is definitely in the folder location. After it fails and you try uploading again, it gives you the same message. When you restart Word and upload the same doc (the addin uses the title of the document as the filename for the temp saveas file), there is no issue. I have seen many questions like this asked but nothing helps.

    Monday, December 7, 2015 9:01 PM

All replies

  • Hi Luke415,

    What’s the detail code that you used? Could you reproduce that issue in a new add-in project?

    According to your description, you save a temporary in AppData\Local\Temp directory, you may try to save to other directory (e.g. d:\temp) and check the result.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 8, 2015 8:02 AM
    Moderator
  • I believe I have pinpointed where it fails. After creating new instances of my Word addin and trying to upload intermittently (I never fully closed Word, just created a new addin doc and closed the previous one). When I got to Document72.docx and tried to upload it failed.

    From my logs on another users machine it seemed like it was failing at the save as function. When the addin uploads the doc, it creates a new Word doc and copy and pastes all of the info onto it, saves,  closes, then uploads this "temp" doc. When the error happened to the other users a save dialog would pop up with the new "temp" doc but it wouldnt be named like it should by the code, and rather named Document61.docx (or whatever number they were up to).

    I made the SaveAs function in the addin recursive if the temp doc didnt save:

    public void SaveAs(Microsoft.Office.Interop.Word.Document document, string fileName)
            {
                try
                {
                    document.SaveAs(ref fileNameObject,
                        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);
                    
                    if(!File.Exists(fileName))
                    {
                        logger.Info("Document NOT Saved : " + fileName);
                        SaveAs(document, fileName);
                    }
                }
                catch (Exception ex)
                {}

     I thought it would be just a blip if it didnt save the first time, but having just experienced it now it basically went into an infinite loop of not saving the temp doc. What could cause this to happen?
    Thursday, December 10, 2015 8:46 PM
  • Hi Luke415,

    Please share a sample project or detail code on the OneDrive, which could reproduce that issue. We will check it.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 11, 2015 2:10 AM
    Moderator