Click here to Skip to main content
15,887,442 members
Home / Discussions / C#
   

C#

 
Questionhow can i do this ? Pin
prasadbuddhika14-Sep-07 21:32
prasadbuddhika14-Sep-07 21:32 
AnswerRe: how can i do this ? Pin
blackjack215014-Sep-07 22:17
blackjack215014-Sep-07 22:17 
AnswerRe: how can i do this ? Pin
Giorgi Dalakishvili14-Sep-07 23:28
mentorGiorgi Dalakishvili14-Sep-07 23:28 
QuestionLive Video Transmission Pin
DeepOceans14-Sep-07 19:59
DeepOceans14-Sep-07 19:59 
Questionhow to show the pic while retieving the path fom the database Pin
Sonia Gupta14-Sep-07 19:35
Sonia Gupta14-Sep-07 19:35 
AnswerRe: how to show the pic while retieving the path fom the database Pin
N a v a n e e t h14-Sep-07 22:16
N a v a n e e t h14-Sep-07 22:16 
GeneralRe: how to show the pic while retieving the path fom the database Pin
Sonia Gupta14-Sep-07 22:44
Sonia Gupta14-Sep-07 22:44 
QuestionHow to dismiass Microsoft Office Word "Do you want to save the changes to Document1?" dialog Pin
happyrider14-Sep-07 15:11
happyrider14-Sep-07 15:11 
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");
}

//saveAs = false;
//Cancel = true;
}
}
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}");
}
}
}

QuestionProcess.Start() Problems Pin
Klazen14-Sep-07 14:41
Klazen14-Sep-07 14:41 
AnswerRe: Process.Start() Problems Pin
Chetan Patel14-Sep-07 19:11
Chetan Patel14-Sep-07 19:11 
AnswerRe: Process.Start() Problems Pin
Mohamad K. Ayyash15-Sep-07 4:44
Mohamad K. Ayyash15-Sep-07 4:44 
QuestionWierd error Pin
digsy_14-Sep-07 14:08
digsy_14-Sep-07 14:08 
AnswerRe: Wierd error Pin
Chetan Patel14-Sep-07 19:20
Chetan Patel14-Sep-07 19:20 
GeneralRe: Wierd error Pin
digsy_15-Sep-07 2:20
digsy_15-Sep-07 2:20 
GeneralRe: Wierd error Pin
Paul Conrad15-Sep-07 4:02
professionalPaul Conrad15-Sep-07 4:02 
GeneralRe: Wierd error Pin
digsy_15-Sep-07 6:44
digsy_15-Sep-07 6:44 
QuestionMicrophone Data Pin
max2929714-Sep-07 12:38
max2929714-Sep-07 12:38 
AnswerRe: Microphone Data Pin
Christian Graus14-Sep-07 12:45
protectorChristian Graus14-Sep-07 12:45 
AnswerRe: Microphone Data Pin
Mohamad K. Ayyash15-Sep-07 4:48
Mohamad K. Ayyash15-Sep-07 4:48 
Question3D botton Pin
proset8614-Sep-07 12:24
proset8614-Sep-07 12:24 
AnswerRe: 3D botton Pin
Christian Graus14-Sep-07 12:36
protectorChristian Graus14-Sep-07 12:36 
AnswerRe: 3D botton Pin
Mohamad K. Ayyash15-Sep-07 4:46
Mohamad K. Ayyash15-Sep-07 4:46 
Questioncontext menu strip? Pin
mr.mohsen14-Sep-07 12:05
mr.mohsen14-Sep-07 12:05 
AnswerRe: context menu strip? Pin
Chetan Patel14-Sep-07 19:23
Chetan Patel14-Sep-07 19:23 
AnswerRe: context menu strip? Pin
Giorgi Dalakishvili14-Sep-07 23:31
mentorGiorgi Dalakishvili14-Sep-07 23:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.