using System; |
using System.Collections.Generic; |
using System.ComponentModel; |
using System.Data; |
using System.Drawing; |
using System.Text; |
using System.Windows.Forms; |
using Microsoft.Office; |
using Microsoft.Office.Interop.Word; |
using Word = Microsoft.Office.Interop.Word; |
namespace CreditRequest |
{ |
public partial class CreditRequest : Form |
{ |
int iTotalFields = 0; |
public CreditRequest() |
{ |
InitializeComponent(); |
} |
private void btn_Generate_Click(object sender, EventArgs e) |
{ |
Object oMissing = System.Reflection.Missing.Value; |
Object oTrue = true; |
Object oFalse = false; |
Word.Application oWord = new Word.Application(); |
Word.Document oWordDoc = new Word.Document(); |
oWord.Visible = true; |
Object oTemplatePath = "C:\\Template.dot"; |
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); |
foreach (Word.Field myMergeField in oWordDoc.Fields) |
{ |
iTotalFields++; |
Word.Range rngFieldCode = myMergeField.Code; |
String fieldText = rngFieldCode.Text; |
if (fieldText.StartsWith(" MERGEFIELD")) |
{ |
Int32 endMerge = fieldText.IndexOf("\\"); |
Int32 fieldNameLength = fieldText.Length - endMerge; |
String fieldName = fieldText.Substring(11, endMerge - 11); |
fieldName = fieldName.Trim(); |
if (fieldName == "Name") |
{ |
myMergeField.Select(); |
oWord.Selection.TypeText(txt_CustName.Text); |
} |
if (fieldName == "Date") |
{ |
myMergeField.Select(); |
oWord.Selection.TypeText(dtp_CreditDate.Text); |
} |
if (fieldName == "Country") |
{ |
myMergeField.Select(); |
oWord.Selection.TypeText(txt_DestCountry.Text); |
} |
} |
} |
} |
} |
} Refer : http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/1a100644-da0f-482c-8042-339a8ff1658a |
Tuesday, December 20, 2011
MS word Automation
Labels:
asp.net . c#. .net,
ms word,
pdf,
word automation
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment