Search code examples
c#itextdoc

How to create doc 2003 file using C#


Is there any way I can create doc file using C#. I have tried it but it does not open in 2003 but it opens in word 2007?

I have been using this code but the doc file does not open in word 2003:

//writing to doc file
object oMissing = System.Reflection.Missing.Value;               
Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word._Document oDoc = new Microsoft.Office.Interop.Word.Document();

oWord.Visible = false;

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

//Insert a paragraph at the beginning of the document.
Microsoft.Office.Interop.Word.Paragraph oPara1;

oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = text;

object fileName = "C:\\question.doc";
oDoc.SaveAs(ref fileName,
    ref oMissing, ref oMissing,
    ref oMissing, ref oMissing,
    ref oMissing, ref oMissing,
    ref oMissing, ref oMissing,
    ref oMissing, ref oMissing);

Solution

  • you need to use wdFormatDocument97 as the second parameter of SaveAs() - for details see http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.saveas.aspx and http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdsaveformat.aspx

    EDIT - as per comment:

    use

    object vFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97;
    oDoc.SaveAs(ref fileName,
            ref vFormat, ref oMissing,
            ref oMissing, ref oMissing,
            ref oMissing, ref oMissing,
            ref oMissing, ref oMissing,
            ref oMissing, ref oMissing);