mackie duenas

mackie duenas

  • NA
  • 28
  • 15.8k

generate letter using c# in visual studio

Apr 23 2014 10:23 PM
i have a problem on these codes trying to write official letters. My requirements were letter template & content that can be changed any time and application will help to get only inputs like sent to name, organization name, date, etc. and after that when user presses finish button, the letter should be ready for print out along with predefined content & inputs. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;//<- this is what I am talking about
namespace generateletter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
//  create offer letter
                try
                {
//  Just to kill WINWORD.EXE if it is running
                    killprocess("winword");
//  copy letter format to temp.doc
                    File.Copy("C:\\OfferLetter.doc", "c:\\temp.doc", true);  
//  create missing object
                    object missing = Missing.Value;
//  create Word application object
                    Word.Application wordApp = new Word.ApplicationClass();
//  create Word document object
                    Word.Document aDoc = null;
//  create & define filename object with temp.doc
                    object filename = "c:\\temp.doc";
//  if temp.doc available
                    if (File.Exists((string)filename))  
                    {
                        object readOnly = false;
                        object isVisible = false;
//  make visible Word application
                        wordApp.Visible = false;
//  open Word document named temp.doc
                        aDoc = wordApp.Documents.Open(ref filename, ref missing, 
ref readOnly, ref missing,ref missing, ref missing, 
ref missing, ref missing, ref missing, ref missing, 
ref missing,ref isVisible, ref missing, ref missing, 
ref missing, ref missing);
                        aDoc.Activate();
//  Call FindAndReplace()function for each change
                        this.FindAndReplace(wordApp, "<Date>", dtpDate.Text);
                        this.FindAndReplace(wordApp, "<Name>", txName.Text.Trim());
                        this.FindAndReplace(wordApp, "<Subject>", 
txtSubject.Text.Trim());
//  save temp.doc after modified
                        aDoc.Save();
                    }
                    else
                        MessageBox.Show("File does not exist.", 
"No File", MessageBoxButtons.OK, 
MessageBoxIcon.Information);
                    killprocess("winword");
                }
                catch (Exception)
                {
                    MessageBox.Show("Error in process.", "Internal Error", 
MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
}


        private void FindAndReplace(Word.Application wordApp, 
        object findText, object replaceText)
{  
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;
            wordApp.Selection.Find.Execute(ref findText, ref matchCase,
                ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
                ref matchAllWordForms, ref forward, ref wrap, ref format,
                ref replaceText, ref replace, ref matchControl);
}
        }
    }



Answers (6)