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
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();
}
}
}
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);
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