This project has moved. For the latest updates, please go here.
/*  
   using events in Word
   taken from Example06.cs
*/ 
public partial class Form1 : Form
{
   private delegate void UpdateEventTextDelegate(string Message);
   UpdateEventTextDelegate _updateDelegate;

   public Form1()
   {
       InitializeComponent();
       _updateDelegate = new UpdateEventTextDelegate(UpdateTextbox);
   }

   private void button1_Click(object sender, EventArgs e)
   {
       // start word and turn off msg boxes
       Word.Application wordApplication = new Word.Application();
       wordApplication.DisplayAlerts = NetOffice.WordApi.Enums.WdAlertLevel.wdAlertsNone; 

       /*
       we register some events. note: the event trigger was called from word, 
means an other Thread */
wordApplication.NewDocumentEvent += new NetOffice.WordApi.Application_NewDocumentEventHandler(
word_NewDocumentEvent); wordApplication.DocumentBeforeCloseEvent += new NetOffice.WordApi.Application_DocumentBeforeCloseEventHandler(
word_DocumentBeforeCloseEvent); // add new document and close Word.Document document = wordApplication.Documents.Add(); document.Close(); // close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); } void word_DocumentBeforeCloseEvent(NetOffice.WordApi.Document Doc, ref bool Cancel) { textBoxEvents.BeginInvoke(_updateDelegate,
new object[] { "Event DocumentBeforeClose called." }); Doc.Dispose(); } void word_NewDocumentEvent(NetOffice.WordApi.Document Doc) { textBoxEvents.BeginInvoke(_updateDelegate,
new object[] { "Event NewDocumentEvent called." }); Doc.Dispose(); } private void UpdateTextbox(string message) { textBoxEvents.AppendText(message + "\r\n"); } }

Last edited Jun 13, 2012 at 9:04 AM by SebastianDotNet, version 6

Comments

No comments yet.