Integrating MS Word into application

A

Andrew Mercer

Hi,

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.

Any assistance greatly appreaciated.

Thanks Andrew Mercer
 
C

Cindy M.

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
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

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

Andrew Mercer

Hi,

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:

Code:

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;
try
{
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
oMissing,
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
oMissing);
oWordDoc2.Activate();

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

// The event handlers.
private static void oWord_DocumentBeforeClose(WORD.Document doc, ref
bool Cancel)
{
System.Windows.Forms.MessageBox.Show(
"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
design.

Any suggestions/help greatly appreciated

Thanks Again

Andrew
 
C

Cindy M.

Hi =?Utf-8?B?QW5kcmV3IE1lcmNlcg==?=,
however when I add the code
to my application the code for the event does not get accessed:
The event works on my system... But I use program against the
Word.Application and Word.Document and NOT Word.ApplicationClass
and Word._Document. Andrew Whitechapel explains about this in his
book on Office development with Microsoft .NET (don't have it to
hand, today). I can't remember the details, but it has something
to do with how the PIAs need to make some things public in order
for them to work, but you shouldn't program against them.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top