Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can i create once a word template?
Then fill all data on word template field?

please guide me...and provide some code if you know.

What I have tried:

Object oMissing = System.Reflection.Missing.Value;
Object oTemplatePath = "D:\\MyTemplate.dotx";

Application wordApp = new Application();
Document wordDoc = new Document();

wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

foreach (Field myMergeField in wordDoc.Fields)
{


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

// 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 == "Name")
{

myMergeField.Select();

wordApp.Selection.TypeText("Vibhusha");

}

}

}
wordDoc.SaveAs("myfile.doc");
wordApp.Documents.Open("myFile.doc");
wordApp.Application.Quit();



--> i have to do this but on bellow code i get some error,

Application wordApp = new Application();--->System.web.ui.page.application is a 'Property' but is used like a 'type'


Document wordDoc = new Document();--->The type or namespace name 'Document' could not be found(are you missing a using directive or an assembly reference?
Posted
Updated 4-Aug-16 20:16pm

I think you are getting this error because you have not referenced any "Microsoft Word Object library".
C#
This will automatically add VBIDE, Microsoft.Office.Core along with Microsoft.Office.Interop.Word as references.


After that, you need to use the required namespace like
C#
using Word = Microsoft.Office.Interop.Word;


For more reference, go through the below link:

Edit Microsoft Word Document From Your Application[^]

This may help you.
 
Share this answer
 
wordApp is not Word, just application, see the MSDN pages on how to use Word and Excel (the Word part is at the bottom). Also reading quickly you never use Document, which is not a Word document anyway.

How to: Access Office Interop Objects by Using Visual C# Features (C# Programming Guide)[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900