Apply template styles to Word Doc - C#

Posted by Ashokn under C# on 8/31/2011 | Points: 10 | Views : 2180 | Status : [Member] | Replies : 0
Hi,

I have a table in Word document and I am attempting to apply styles from a template where I have defined table styles for template(like Table heading-C,Tabletext-C etc) for table column texts ceneter-aligned and table text center aligned.

I am using below code to apply template table styles to my word doc table style.But below code is only copying these styles to newly created testResult.docx but styles are not applied to table.Kindly suggest solution/alternate for this.


public static void Test(object wordfilePath, string templateFilePath)
{
object o_nullobject = System.Reflection.Missing.Value;

w.Application wordApp = new w.ApplicationClass();
w.Document doc = null;
doc = wordApp.Documents.Open(ref wordfilePath, ref o_nullobject, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject);
doc.CopyStylesFromTemplate(templateFilePath);
//doc.Activate();
doc.UpdateStyles();
object output = @"D:\testResult.docx";
doc.SaveAs(ref output, ref o_nullobject, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject);
doc.Close(ref o_nullobject, ref o_nullobject, ref o_nullobject);
wordApp.Quit(ref o_nullobject, ref o_nullobject, ref o_nullobject);
}

I have Called above function as shown below

Class1.Test("D:\\MyWord.docx" ,"D:\Mytemplate.dotx");

Thanks in advance...




Responses

(No response found.)

Login to post response