Re: Integrating MS Word into application


Thanks this has been helpful as an understanding however when I add the code
to my application the code for the event does not get accessed:


using WORD = Microsoft.Office.Interop.Word;
// Open the document
public static void OpenWordFile(string CaseNumber)
object oMissing = System.Reflection.Missing.Value;
FileInfo file = new FileInfo(filename);
if (file.Exists)
WORD.ApplicationClass oWordApp2 = null;
WORD._Document oWordDoc2 = null;
object fileName2 = (object)[filename];
object readOnly = false;
object isVisible = true;
oWordApp2 = new WORD.ApplicationClass();
oWordApp2.Visible = true;
oWordApp2.Height = oWordApp2.System.VerticalResolution;
oWordApp2.Width = oWordApp2.System.HorizontalResolution;
oWordDoc2 = oWordApp2.Documents.Open(ref fileName2, ref
ref readOnly, ref oMissing, ref oMissing, ref
oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref isVisible,
ref oMissing, ref oMissing, ref oMissing, ref

oWordApp2.DocumentBeforeClose +=
catch (COMException ce)
Util.UpdateMsgFile("Failure opening file " +
filename + "\n StackTrace: " + ce.StackTrace);
if (oWordDoc2 != null)
oWordDoc2.Close(ref oMissing, ref oMissing, ref
oWordDoc2 = null;
if (oWordApp2 != null)
oWordApp2.Quit(ref oMissing, ref oMissing, ref
System.Windows.Forms.MessageBox.Show("ERROR: During data
download or creation of " +

// The event handlers.
private static void oWord_DocumentBeforeClose(WORD.Document doc, ref
bool Cancel)
"DocumentBeforeClose ( You are closing " + doc.Name + ")");

Am I misunderstanding where the code should go? The above code uses sample
code from the msn web site and it suggests that the above is an acceptable

Any suggestions/help greatly appreciated

Thanks Again


Andrew Mercer

"Cindy M." wrote:

Hi =?Utf-8?B?QW5kcmV3IE1lcmNlcg==?=,

Well, the Word application exposes a DocumentBeforeClosed event you could sink.

I have a standalone .NET windows application that uses MS Office .NET
programmability to create, build and open Word documents and in some cases
automatically print the same documents.

I have a thread running in the application to check if the current opened
word document (in a specified directory)has been closed (basically in simple
terms it checks to see if the ~$... version of the document has gone).

When this is true then the application does another action.
However I am sure this is not the best way.

My question is: Is it possible that when closing an open word document that
a call can be made from word to my standalone application which can then do
the required action?

I have searched the web without success - the integration examples only seem
to show the creation etc... of word documents.

Cindy Meister
INTER-Solutions, Switzerland (last update Jun 17 2005)

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :-)