using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using Outlook = Microsoft.Office.Interop.Outlook;namespace WorkflowActivitys.Resources
{
class WordOfficeTasks
{
private ApplicationClass WordApp = new ApplicationClass(); // new instance of word
private Document WordDoc = new Document(); // new instance of document
public void ProduceHmynSheet(DataSet Arrangements)
{
object missing = System.Reflection.Missing.Value; // 'void' value
object filename = @"D:\hymnsheet.dot"; // word template to use
WordApp.Visible = false; // tell word not to show itself
WordDoc = WordApp.Documents.Add( // load the template into a document workspace
ref filename, // and reference it through our myWordDoc
ref missing,
ref missing,
ref missing);
// string of tags that exsist in word template
string[] tags = new string[] {"<location_line1>","<location_line2>","<first_name>","<surname>",
"<year_birth>","<year_death>","<date_of_service>"};
// sting of values to exchange tags for
string[] values = new String[] {Arrangements.Tables["Interment_Location"].Rows[0]["Location_Name"].ToString(),
Arrangements.Tables["Interment_Location"].Rows[0]["Address"].ToString(),
Arrangements.Tables["Funeral_Arrangements"].Rows[0]["Deceased_Fornames"].ToString().ToUpper(),
Arrangements.Tables["Funeral_Arrangements"].Rows[0]["Deceased_Surname"].ToString().ToUpper(),
((DateTime)Arrangements.Tables["Funeral_Arrangements"].Rows[0]["Deceased_Date_of_Birth"]).ToString("yyyy"),
((DateTime)Arrangements.Tables["Funeral_Arrangements"].Rows[0]["Deceased_Date_Time_of_Death"]).ToString("yyyy"),
((DateTime)Arrangements.Tables["Funeral_Arrangements"].Rows[0]["Interment_Date_Time"]).ToString("dddd d MMMM yyyy")};
object replaceAll = Word.WdReplace.wdReplaceAll;
for (int i = 0; i < tags.Length; i++)
{
// find tag from tag[] string array
WordApp.Selection.Find.Text = tags
;
// replace with value from values[] string array
WordApp.Selection.Find.Replacement.Text = values
;
// call find and replace
WordApp.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}
//Button[] Hymns = new Button[Arrangements.Tables["Hymns"].Rows.Count];
//for (int i = 0; i < Arrangements.Tables["Hymns"].Rows.Count; i++)
//{
// Hymns
= new Button();
// Hymns
.Text = Arrangements.Tables["Hymns"].Rows
["Hymn_ID"].ToString();
// //WordDoc
// //ActP.Controls.Add(Hymns
);
//}
WordApp.Visible = true;
}
This code does rely on a few more classes and then an SQL database but it should give you a good enough idea of what i'm doing