S
skywalker
My application will generate a word document as report. The application
created Word Application and Document objects.
When users click on Close or the "X" icon on the up right corner. The
application will intercept word DocumentBeforeCloseEvent, to ask users Save,
No or Cancel to the document changes. Everything works fine except when users
click on "Cancel", Word still displays it's own Dialog for users to select
Yes, No or Cancel for saving the document changes.
Is there anyway to prevent this Word dialog because my application already
process uses decision?
Here is part of the code:
Word_DocumentBeforeClose is used to process users decision.
The last part of If in else is to process when Cancel is clicked.
namespace HandleWordEvnts
{
public partial class Form1 : Form
{
#region properties
Word.ApplicationClass wordApp;
Word.Document wordDoc;
string docName;
#endregion properties
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//===== Create a new document in Word ==============
// Create an instance of Word and make it visible.
wordApp = new Word.ApplicationClass();
wordApp.Visible = true;
// Local declarations.
Object oMissing = System.Reflection.Missing.Value;
// Add a new document.
wordDoc = wordApp.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing); // Clean document
// Add text to the new document.
wordDoc.Content.Text = "Handle Events for Microsoft Word Using
C#.";
docName = wordDoc.Name;
//============ Set up the event handlers ===============
wordApp.DocumentBeforeClose +=
new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(
Word_DocumentBeforeClose);
}
// The event handlers.
private DialogResult showSaveFileDlg(ref string fileName, ref string
filePath)
{
DialogResult dlgRes = DialogResult.None;
object locker = new object();
Thread.Sleep(1000); //time needed for dialog to display
lock (locker)
{
saveFileDialog1.Title = "App Save As";
saveFileDialog1.FileName = filePath != "" ? (filePath + "\\"
+ fileName) : fileName; //init full name
saveFileDialog1.InitialDirectory = filePath;
saveFileDialog1.Filter = "Word Document (*.doc)|*.doc| Plain
Text (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
dlgRes = saveFileDialog1.ShowDialog();
if (dlgRes == DialogResult.Cancel) return dlgRes;
fileName =
saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.LastIndexOf("\\")
+ 1);
filePath = saveFileDialog1.FileName.Substring(0,
saveFileDialog1.FileName.LastIndexOf("\\"));
return dlgRes;
}
}
private void Word_DocumentBeforeClose(Word.Document doc, ref bool
Cancel)
{
docName = doc.Name;
string fileName = doc.Name;
string filePath = doc.Path;
// ask user to save or not
DialogResult dlgRes = DialogResult.None;
AskSaveCnanges askSave = new AskSaveCnanges();
askSave.setTxtBox1 = filePath != "" ? (filePath + "\\" +
fileName) : fileName;
askSave.setTxtBox1Readonly = true;
askSave.ShowDialog();
if (askSave.DlgRes == DialogResult.Yes) //user agrees to save
{
Thread showWordDlg = new Thread(delegate() { dlgRes =
showSaveFileDlg(ref fileName, ref filePath); });
showWordDlg.SetApartmentState(ApartmentState.STA);
showWordDlg.Name = "DocClose";
showWordDlg.Start();
showWordDlg.Join();
if (dlgRes == DialogResult.OK) //user agrees to save
{
MessageBox.Show("DocumentBeforeClose ( Saved " +
doc.Name + " )","APP");
//Save the file, use default values except for filename
object ofileName = filePath + "\\" + fileName;
object optional = Missing.Value;
try
{
doc.SaveAs(ref ofileName, ref optional, ref
optional, ref optional,
ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional, ref
optional);
//doc.Save();
doc.Saved = true;
object saveChanges = true;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
wordApp.Quit(ref saveChanges, ref originalFormat,
ref routeDocument);
}
catch (COMException e)
{
MessageBox.Show(e.Message,"ApP.DocumentBeforeClose");
}
}
}
else if (askSave.DlgRes == DialogResult.No)
{
MessageBox.Show("DocumentBeforeClose ( Not Saved " +
doc.Name + " )","APP");
Cancel = true;
object saveChanges = false;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
doc.Close(ref saveChanges, ref originalFormat, ref
routeDocument);
wordApp.Quit(ref saveChanges, ref originalFormat, ref
routeDocument);
}
else
{
MessageBox.Show("DocumentBeforeClose ( Canceled " + doc.Name
+ " )","APP");
//When users cilck on Cancel button.
// Cancel is set to true but Word still displays it's own dialog.
Cancel = true;
doc.Saved = false;
wordApp.Activate();
Thread.Sleep(5000);
//send key does not work either
SendKeys.SendWait("{RIGHT}{RIGHT}{ENTER}");
}
}
}
created Word Application and Document objects.
When users click on Close or the "X" icon on the up right corner. The
application will intercept word DocumentBeforeCloseEvent, to ask users Save,
No or Cancel to the document changes. Everything works fine except when users
click on "Cancel", Word still displays it's own Dialog for users to select
Yes, No or Cancel for saving the document changes.
Is there anyway to prevent this Word dialog because my application already
process uses decision?
Here is part of the code:
Word_DocumentBeforeClose is used to process users decision.
The last part of If in else is to process when Cancel is clicked.
namespace HandleWordEvnts
{
public partial class Form1 : Form
{
#region properties
Word.ApplicationClass wordApp;
Word.Document wordDoc;
string docName;
#endregion properties
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//===== Create a new document in Word ==============
// Create an instance of Word and make it visible.
wordApp = new Word.ApplicationClass();
wordApp.Visible = true;
// Local declarations.
Object oMissing = System.Reflection.Missing.Value;
// Add a new document.
wordDoc = wordApp.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing); // Clean document
// Add text to the new document.
wordDoc.Content.Text = "Handle Events for Microsoft Word Using
C#.";
docName = wordDoc.Name;
//============ Set up the event handlers ===============
wordApp.DocumentBeforeClose +=
new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(
Word_DocumentBeforeClose);
}
// The event handlers.
private DialogResult showSaveFileDlg(ref string fileName, ref string
filePath)
{
DialogResult dlgRes = DialogResult.None;
object locker = new object();
Thread.Sleep(1000); //time needed for dialog to display
lock (locker)
{
saveFileDialog1.Title = "App Save As";
saveFileDialog1.FileName = filePath != "" ? (filePath + "\\"
+ fileName) : fileName; //init full name
saveFileDialog1.InitialDirectory = filePath;
saveFileDialog1.Filter = "Word Document (*.doc)|*.doc| Plain
Text (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
dlgRes = saveFileDialog1.ShowDialog();
if (dlgRes == DialogResult.Cancel) return dlgRes;
fileName =
saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.LastIndexOf("\\")
+ 1);
filePath = saveFileDialog1.FileName.Substring(0,
saveFileDialog1.FileName.LastIndexOf("\\"));
return dlgRes;
}
}
private void Word_DocumentBeforeClose(Word.Document doc, ref bool
Cancel)
{
docName = doc.Name;
string fileName = doc.Name;
string filePath = doc.Path;
// ask user to save or not
DialogResult dlgRes = DialogResult.None;
AskSaveCnanges askSave = new AskSaveCnanges();
askSave.setTxtBox1 = filePath != "" ? (filePath + "\\" +
fileName) : fileName;
askSave.setTxtBox1Readonly = true;
askSave.ShowDialog();
if (askSave.DlgRes == DialogResult.Yes) //user agrees to save
{
Thread showWordDlg = new Thread(delegate() { dlgRes =
showSaveFileDlg(ref fileName, ref filePath); });
showWordDlg.SetApartmentState(ApartmentState.STA);
showWordDlg.Name = "DocClose";
showWordDlg.Start();
showWordDlg.Join();
if (dlgRes == DialogResult.OK) //user agrees to save
{
MessageBox.Show("DocumentBeforeClose ( Saved " +
doc.Name + " )","APP");
//Save the file, use default values except for filename
object ofileName = filePath + "\\" + fileName;
object optional = Missing.Value;
try
{
doc.SaveAs(ref ofileName, ref optional, ref
optional, ref optional,
ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional, ref
optional);
//doc.Save();
doc.Saved = true;
object saveChanges = true;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
wordApp.Quit(ref saveChanges, ref originalFormat,
ref routeDocument);
}
catch (COMException e)
{
MessageBox.Show(e.Message,"ApP.DocumentBeforeClose");
}
}
}
else if (askSave.DlgRes == DialogResult.No)
{
MessageBox.Show("DocumentBeforeClose ( Not Saved " +
doc.Name + " )","APP");
Cancel = true;
object saveChanges = false;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
doc.Close(ref saveChanges, ref originalFormat, ref
routeDocument);
wordApp.Quit(ref saveChanges, ref originalFormat, ref
routeDocument);
}
else
{
MessageBox.Show("DocumentBeforeClose ( Canceled " + doc.Name
+ " )","APP");
//When users cilck on Cancel button.
// Cancel is set to true but Word still displays it's own dialog.
Cancel = true;
doc.Saved = false;
wordApp.Activate();
Thread.Sleep(5000);
//send key does not work either
SendKeys.SendWait("{RIGHT}{RIGHT}{ENTER}");
}
}
}