views:

39

answers:

1

I am using below code to generate word document using assp.net/c#.net, please tell me where i am missing it is generating word file and all contents are their.. but problem is that when i open it it gives me error : "The Office Open XML file filename.doc cannot be opened because there are problems with the contents" and after clicking ok i get my required file.. plez so plez tell me how to remove this error.

in error details :The file is corrupt and cannot be opened

after it again gets a error message: "word found unreadable content in myfile. do you want to recover the contents of this document? if you trust the source of ths document, click yes"

using System;
 using System.Collections.Generic;
using System.Text;
using Microsoft.Office;
using Microsoft.Office.Interop.Word;
 public class clsWordLetterGenerator
{
    private int m_Enquiryid;
    private DateTime m_EnquiryDate;
    private string m_Filename;
    private string m_Templatepath;
    private string m_Templatename;
    private string m_Lettertext;
    private string m_Recieptentfirstname;
    private string m_Recieptentlastname;
    private DateTime m_Letterdate;
    private string m_Generatedfilepath;
    private string m_Generatedfilename;
    private string m_Subjecttext;
    private Microsoft.Office.Interop.Word.Application oWord;
    private Microsoft.Office.Interop.Word.Document oWordDoc;

    public string FileName
    {
        get { return m_Filename; }
        set { m_Filename = value; }
    }

    public string LetterText
    {
        get { return m_Lettertext; }
        set { m_Lettertext = value; }
    }

    public string RecieptentFirstName
    {
        get { return m_Recieptentfirstname; }
        set { m_Recieptentfirstname = value; }
    }

    public string RecieptentLastName
    {
        get { return m_Recieptentlastname; }
        set { m_Recieptentlastname = value; }
    }

    public string SubjectText
    {
        get { return m_Subjecttext; }
        set { m_Subjecttext = value; }
    }

    public string GenerateFileName
    {
        get { return m_Generatedfilename; }
        set { m_Generatedfilename = value; }
    }

    public string GenerateFilePath
    {
        get { return m_Generatedfilepath; }
        set { m_Generatedfilepath = value; }
    }

    public string TemplatePath
    {
        get { return m_Templatepath; }
        set { m_Templatepath = value; }
    }

    public string TemplateName
    {
        get { return m_Templatename; }
        set { m_Templatename = value; }
    }

    public int EnquiryId
    {
        get { return m_Enquiryid; }
        set { m_Enquiryid = value; }
    }

    public DateTime EnquiryDate
    {
        get { return m_EnquiryDate; }
        set { m_EnquiryDate = value; }
    }


    public clsWordLetterGenerator()
    {
          m_Letterdate = DateTime.Now;
        m_Filename = Guid.NewGuid().ToString();
    }

    public void GenerateLetter()
    {
        oWord = new Microsoft.Office.Interop.Word.Application();

        oWordDoc = new Microsoft.Office.Interop.Word.Document();

        //OBJECT OF MISSING "NULL VALUE"

        Object oMissing = System.Reflection.Missing.Value;

        //OBJECTS OF FALSE AND TRUE

        Object oTrue = true;

        Object oFalse = false;

        //CREATING OBJECTS OF WORD AND DOCUMENT

        //MAKING THE APPLICATION VISIBLE

        //oWord.Visible = true;

        //ADDING A NEW DOCUMENT TO THE APPLICATION

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


        oMissing = System.Reflection.Missing.Value;



        //OBJECTS OF FALSE AND TRUE

        oTrue = true;

        oFalse = false;

        //CREATING OBJECTS OF WORD AND DOCUMENT




        //THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE

        Object oTemplatePath = m_Templatepath + "\\" + m_Templatename;

        //ADDING A NEW DOCUMENT FROM A TEMPLATE

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

        int iTotalFields = 0;
        foreach (Microsoft.Office.Interop.Word.Field myMergeField in oWordDoc.Fields)
        {

            iTotalFields++;

            Microsoft.Office.Interop.Word.Range rngFieldCode = myMergeField.Code;

            String fieldText = rngFieldCode.Text;



            // ONLY GETTING THE MAILMERGE FIELDS

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

                // THE TEXT COMES IN THE FORMAT OF

                // MERGEFIELD  MyFieldName  \\* MERGEFORMAT

                // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

                Int32 endMerge = fieldText.IndexOf("\\");

                Int32 fieldNameLength = fieldText.Length - endMerge;

                //String fieldName = fieldText.Substring(11, endMerge - 11);
                String fieldName = fieldText.Replace("MERGEFIELD", "");

                // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE

                fieldName = fieldName.Trim();


                // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//

                // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

                //if (fieldName.ToUpper()  == "FIRST_NAME")
                //{

                //    myMergeField.Select();

                //    oWord.Selection.TypeText("Bhaskar");

                //}

                switch (fieldName.ToUpper())
                {
                    case "FIRST_NAME":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Recieptentfirstname);
                        break;

                    case "LAST_NAME":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Recieptentlastname);
                        break;

                    case "LETTERDATE":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Letterdate.ToShortDateString());
                        break;

                    case "GREETINGLINE":
                        myMergeField.Select();
                        oWord.Selection.TypeText(" " + m_Enquiryid.ToString() + " Dated " + m_EnquiryDate.ToString());
                        break;


                    case "LETTERSUBJECT":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Subjecttext);
                        break;

                    case "LETTERBODY":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Lettertext);
                        break;

                    default:
                        break;
                }


            }
            else
            {
                if (fieldText.ToUpper().Trim().Contains("GREETING"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(" " + m_Enquiryid.ToString() + " Dated " + m_EnquiryDate.ToString("dd-MM-yyyy"));
                }
            }

        }

        //SETTING THE VISIBILITY TO TRUE

        //oWord.Visible = true;
    }

    public  void SaveFile()
    {
        Object oSaveAsFile = (Object)m_Generatedfilepath + "\\" + m_Generatedfilename;
        Object oMissing = System.Reflection.Missing.Value;

        oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

            ref oMissing, ref oMissing);
    }

    public void ClearMe()
    {
        Object oMissing = System.Reflection.Missing.Value;
        Object oFalse = false;
        //CLOSING THE FILE
        oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);

        //QUITTING THE APPLICATION
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

    }

}
+1  A: 

Could it be that Microsoft.Office.Interop.Word is for legacy versions of word? If so I would suggest using the features in System.IO.Packaging that deal with reading and writing of Open Xml documents. I wrote an application a few years ago where I take a template Word Open Xml Document and merging data into it to produce thousands of letters, credit notes, and the like. So I know this approach works.

Carnotaurus