how to use "savefiledialog" in event handler "DocumentBeforeClose"

C

cooldoger

I want save word file in my format, so I catch the
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler
and implement it, but in the handler "savefiledialog" cannot be used--nothing
shown on the window.
here is the part of code:

....
Word.Application WordApp = new Word.ApplicationClass();
....
wordDoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref
missing);
....
wordDoc.Application.DocumentBeforeClose += new
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose);
....

public void Application_DocumentBeforeClose(Word.Document doc, ref bool
cancel)
{
Process processWord = new Process();

Process[] processCollection =
Process.GetProcessesByName("WINWORD");
processWord = processCollection[0];

DialogResult drTemp = MessageBox.Show(new
WindowWrapper(processWord.MainWindowHandle), "save the file?", "file",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if(drTemp==DialogResult.Yes)
{
cancel = false;

SaveFileDialog sfdTemp = new SaveFileDialog();
sfdTemp.DefaultExt = ".boc";
if (
sfdTemp.ShowDialog(new
WindowWrapper(processWord.MainWindowHandle)) == DialogResult.OK //the problem
is here
)
{
MessageBox.Show(sfdTemp.FileName);
//do something else

}

}
else if(drTemp==DialogResult.No)
{
cancel = false;
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat =
Word.WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;
doc.Close(ref saveChanges, ref originalFormat, ref
routeDocument);
}
else
{
cancel = true;
return;
}
}
 
C

Cindy M.

Hi =?Utf-8?B?Y29vbGRvZ2Vy?=,
I want save word file in my format, so I catch the
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler
and implement it, but in the handler "savefiledialog" cannot be used--nothing
shown on the window.
here is the part of code:
I'll ask you again: why aren't you using Word's or Office's built-in dialog boxes?
I'd think the FileDialog object would be something you could use that would
automatically appear over the Word application window.
....
Word.Application WordApp = new Word.ApplicationClass();
....
wordDoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref
missing);
....
wordDoc.Application.DocumentBeforeClose += new
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(App
lication_DocumentBeforeClose);
....

public void Application_DocumentBeforeClose(Word.Document doc, ref bool
cancel)
{
Process processWord = new Process();

Process[] processCollection =
Process.GetProcessesByName("WINWORD");
processWord = processCollection[0];

DialogResult drTemp = MessageBox.Show(new
WindowWrapper(processWord.MainWindowHandle), "save the file?", "file",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if(drTemp==DialogResult.Yes)
{
cancel = false;

SaveFileDialog sfdTemp = new SaveFileDialog();
sfdTemp.DefaultExt = ".boc";
if (
sfdTemp.ShowDialog(new
WindowWrapper(processWord.MainWindowHandle)) == DialogResult.OK //the problem
is here
)
{
MessageBox.Show(sfdTemp.FileName);
//do something else

}

}
else if(drTemp==DialogResult.No)
{
cancel = false;
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat =
Word.WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;
doc.Close(ref saveChanges, ref originalFormat, ref
routeDocument);
}
else
{
cancel = true;
return;
}
}

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