Com Exception: Word could not read this document. It may be damaged.

I have a web application that takes some information about a client and creates an email for each client. Everything works fine in my test environment, but on the production server I get an error message stating that the file is damaged. I can open the .dotx file in a word, just fine on the server, but not through the code. Please help. Here is my code:

Object oMissing = System.Reflection.Missing.Value;

            Object oTrue = true;
            Object oFalse = false;

            Word.Application oWord = new Word.Application();
            Word.Document oWordDoc = new Word.Document();


            oWord.Visible = false;

            Object oTemplatePath = Request.PhysicalApplicationPath + "letters\\" + letter.letter_data; //samplehollisletter.dotx";

            oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

            foreach (Word.Field myMergeField in oWordDoc.Fields)
            {

                iTotalFields++;
                Word.Range rngFieldCode = myMergeField.Code;
                String fieldText = rngFieldCode.Text;

                if (fieldText.StartsWith(" MERGEFIELD"))
                {

                    Int32 endMerge = fieldText.IndexOf("\\");
                    Int32 fieldNameLength = fieldText.Length - endMerge;
                    String fieldName = fieldText.Substring(11, endMerge - 11);

                    fieldName = fieldName.Trim();

                    if (fieldName == "letter_title")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(acct.letter_title);
                    }
                    if (fieldName == "account_id")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(acct.account_id);
                    }

                    if (fieldName == "address")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(acct.PEOPLE.home_address + "\r\n" + acct.PEOPLE.home_city + ", " + acct.PEOPLE.home_state + " " + acct.PEOPLE.home_zip);
                    }

                    if (fieldName == "greeting_title")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(acct.greeting_title);
                    }

                    if (fieldName == "service_name")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText((acct.SERVICEs.FirstOrDefault()).service_name);
                    }

                    if (fieldName == "service_date")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText((acct.SERVICEs.FirstOrDefault()).service_date.ToString());
                    }


                }
            }
            oWordDoc.PrintOut();
            oWordDoc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
            Thread.Sleep(2000);
            oWord.Quit();

Error:

Server Error in '/Tracker2' Application. 

Word . . : * . * Text Recovery. : -. .

: System.Runtime.InteropServices.COMException: Word . . : * . * Text Recovery.

:

-. .

:

[COMException (0x800a141f): Word . . : * . * .]  Microsoft.Office.Interop.Word.Documents.Add(Object & Template, Object & NewTemplate, Object & DocumentType, Object & Visible) +0  Tracker.RunLetter2.Button1_Click ( , EventArgs e) C:\Users\Ethan\Documents\Visual Studio 2010\Projects\EstateTracker\Tracker\RunLetter2.aspx.cs: 52  System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3691

+3
2

, WCF BasicHTTPBinding. , IIS, . . , Windows, , WCF .

, , , , . . - . , 60 . , , .

+2

, , , , , .

1 - IIS , , Identity LocalSystem

2 - Desktop : C:\Windows\System32\Config\systemprofile C:\Windows\SysWOW64\config\systemprofile

IIS: IIS_IUSRS

" ", , IIS Word, .

-.

+7

Source: https://habr.com/ru/post/1768088/


All Articles