2

I have been using the following to code to write in word file but not able to store the word file. Is there any way to store the word file using C# ?

object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
                //Start Word and create a new document.
                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 = true;

            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 = "Heading 1";
            oPara1.Range.Font.Bold = 1;
            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
2

3 Answers 3

9

You should just be able to use SaveAs.

oDoc.SaveAs("MyFile.doc", 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);

If you are using .NET 4.0 you don't need the oMissings.

S

Sign up to request clarification or add additional context in comments.

1 Comment

+1; beat me to it. But if they aren't using C# 4 then the fileName has to be passed by reference.
4

I just created a new console application using .NET 4 and C#, referenced Microsoft Word Object Library, pasted your code and removed all those ref missing as with .NET 4 and optional parameters are no longer needed, here the final code which really works like a charme:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.Interop.Word;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Word._Application oWord = new Application();

            oWord.Visible = true;

            var oDoc = oWord.Documents.Add();

            //Insert a paragraph at the beginning of the document.
            var paragraph1 = oDoc.Content.Paragraphs.Add();

            paragraph1.Range.Text = "Heading 1";
            paragraph1.Range.Font.Bold = 1;
            paragraph1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.

            oDoc.SaveAs2(@"C:\Temp\TestDocumentWith1Paragraph.docx");

            oWord.Quit();
        }
    }
}

Comments

0

Try this:

var FileName = 'file name with path'

    oWord.ActiveDocument.SaveAs(ref FileName, 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);
    oDoc.Close(ref missing, ref missing, ref missing);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.