private void cmsDocumentOpen_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { // start word if (_wrdApp == null) { _wrdApp = new Microsoft.Office.Interop.Word.Application(); _wrdApp.DocumentBeforeSave += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(_wrdApp_DocumentBeforeSave); _wrdApp.DocumentBeforeClose += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(_wrdApp_DocumentBeforeClose); _wrdApp.WindowActivate += new ApplicationEvents4_WindowActivateEventHandler(_wrdApp_WindowActivate); ((Microsoft.Office.Interop.Word.ApplicationEvents4_Event)_wrdApp).Quit += new ApplicationEvents4_QuitEventHandler(_wrdApp_Quit); } // opening word document based on user selection Document wrdDoc = new Document(); /******************************************************************************** * New Document ********************************************************************************/ if (e.ClickedItem.Equals(cmsDocumentOpenNew)) { wrdDoc = _wrdApp.Documents.Add(ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing); } /******************************************************************************** * Existing Document ********************************************************************************/ else if (e.ClickedItem.Equals(cmsDocumentOpenExisting)) { if (DialogResult.Cancel == openFileDialog.ShowDialog()) { return; } object fileName = openFileDialog.FileName; wrdDoc = _wrdApp.Documents.Open(ref fileName, ref _objMissing, ref _objFalse, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objTrue, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing); } /******************************************************************************** * Stored Document ********************************************************************************/ else if (e.ClickedItem.Equals(cmsDocumentOpenStored)) { if (!MailMerge.StoreDirectoryConfigured() && !MailMerge.StoreDirectoryExists()) { MyMessageBox.Show(this, "MS Word Mail Merge", MyDisplayMessage.MailMergeDirectoryError); return; } frmMailMergeTemplates frm = new frmMailMergeTemplates(); if (DialogResult.Cancel == frm.ShowDialog(this) || string.IsNullOrEmpty(frm.SelectedMailMergeTemplatePath)) { return; } try { object objFileName = MailMerge.CopyTempStoredMailMergeFile(frm.SelectedMailMergeTemplatePath).FullName; wrdDoc = _wrdApp.Documents.Open(ref objFileName, ref _objMissing, ref _objFalse, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref _objTrue, ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing); } catch (IOException) { MessageBox.Show(this, "A document with the same name is already open", "Mail Merge", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } catch (Exception) { MessageBox.Show(this, "Error opening MS Word", "Mail Merge", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } _wrdApp.Visible = true; wrdDoc.Activate(); UpdateGUIState(); }
private void mnuDefendant_Click(object sender, EventArgs e) { Defendant defendant = (Defendant)bindingDefendants.Current; /******************************************************************************** * New ********************************************************************************/ if (sender.Equals(mnuDefendantNew)) { if (CanChangeCurrentDefendant()) { this.SuspendLayout(); if (ucFilter.FilterValue.Length > 0) { bindingDefendants.Filter = ""; ucFilter.FilterValue = ""; } _GridViewState = GridViewState.Adding; bindingDefendants.AddNew(); _GridViewState = GridViewState.None; bindingDefendants.ResetCurrentItem(); this.ResumeLayout(false); ucDefendant.Focus(); } } /******************************************************************************** * Remove ********************************************************************************* * else if( sender.Equals( mnuDefendantRemove ) ) * { * if( DialogResult.OK == MyMessageBox.Show( this, "Defendant", MyDisplayMessage.RemoveConfirm ) ) * { * this.SuspendLayout(); * * try * { * bindingDefendants.RemoveCurrent(); * bindingDefendants.ResetBindings( false ); * this.ucFilter.Focus(); * } * catch( MyException ex ) * { * MyMessageBox.Show( this, "Defendant", MyDisplayMessage.RemoveError, ex ); * } * * bindingDefendants.ResetCurrentItem(); * * this.ResumeLayout( false ); * } * * }*/ /******************************************************************************** * Refresh ********************************************************************************/ else if (sender.Equals(mnuDefendantRefresh)) { if (!defendant.MyState.Equals(MyObjectState.Current)) { if (DialogResult.Cancel == MyMessageBox.Show(this, "Defendant", MyDisplayMessage.RefreshConfirm)) { return; } } this.Cursor = Cursors.WaitCursor; this.SuspendLayout(); ucDefendant.SuspendLayout(); try { defendant.Refresh(); bindingDefendants.ResetBindings(false); ucDefendant.Update(); ucDefendant.Focus(); } catch (MyException ex) { MyMessageBox.Show(this, "Defendant", MyDisplayMessage.RefreshError, ex); } ucDefendant.ResumeLayout(false); this.ResumeLayout(false); this.Cursor = Cursors.Default; } /******************************************************************************** * Save ********************************************************************************/ else if (sender.Equals(mnuDefendantSave)) { ucDefendant.Select(); bindingDefendants.EndEdit(); this.Cursor = Cursors.WaitCursor; this.SuspendLayout(); try { defendant.Save(true); bindingDefendants.Sort = ""; // setting sort string strSort = string.Empty; string strSortOrder = (dgvDefendants.SortOrder == SortOrder.Descending) ? "DESC" : "ASC"; if (dgvDefendants.SortedColumn.DataPropertyName.ToLower() == "lastname") { strSort = "LastName " + strSortOrder + ", FirstName " + strSortOrder; } else { strSort = "FirstName " + strSortOrder + ", LastName " + strSortOrder; } bindingDefendants.Sort = strSort; ResetDataGridViewDefendant(defendant); } catch (MyException ex) { MyMessageBox.Show(this, "Defendant", MyDisplayMessage.SaveError, ex); } catch (ArgumentOutOfRangeException) { MyMessageBox.Show(this, "Payment Arrangement", MyDisplayMessage.PaymentArrangementOverlapping); } this.ResumeLayout(false); this.Cursor = Cursors.Default; } /******************************************************************************** * Cancel ********************************************************************************/ else if (sender.Equals(mnuDefendantCancel)) { this.SuspendLayout(); if (defendant.MyState == MyObjectState.New) { bindingDefendants.RemoveCurrent(); if (bindingDefendants.Count > 0) { bindingDefendants.Position = 0; ucDefendant.Focus(); } else { this.ucFilter.Focus(); } } else { defendant.Reset(); bindingDefendants.ResetCurrentItem(); } this.ResumeLayout(false); } /******************************************************************************** * Notes ********************************************************************************/ else if (sender.Equals(mnuDefendantNotes)) { if (frmNotes == null) { frmNotes = new frmNotes(); frmNotes.DataBindings.Add("Notes", bindingDefendants, "Notes", true, DataSourceUpdateMode.OnPropertyChanged); frmNotes.FormClosed += new FormClosedEventHandler(frmNotes_FormClosed); } if (!frmNotes.Visible) { frmNotes.Show(this); } else { frmNotes.Focus(); } } /******************************************************************************** * Mail Merge ********************************************************************************/ else if (sender.Equals(mnuDefendantMailMerge)) { if (!MailMerge.StoreDirectoryConfigured() && !MailMerge.StoreDirectoryExists()) { MyMessageBox.Show(this, "MS Word Mail Merge", MyDisplayMessage.MailMergeDirectoryError); return; } frmMailMergeTemplates frm = new frmMailMergeTemplates(); if (DialogResult.Cancel == frm.ShowDialog(this) || string.IsNullOrEmpty(frm.SelectedMailMergeTemplatePath)) { return; } this.Cursor = Cursors.WaitCursor; int defendantid = ((Defendant)bindingDefendants.Current).ID; int planid = ucPlans.PlanId; string templatePath = frm.SelectedMailMergeTemplatePath; try { MailMerge.PerformMailMerge(defendantid, planid, templatePath); } catch (Exception ex) { MyMessageBox.Show(this, "Mail Merge", MyDisplayMessage.MailMergeDocumentMergeError, new MyException(ex.Message.ToString(), MyErrorType.MailMergeError, ex)); } this.Cursor = Cursors.Default; } /******************************************************************************** * Make Payment ********************************************************************************/ else if (sender.Equals(mnuDefendantPayment)) { BindingSource bindingPayments = new BindingSource(new BindingSource(bindingDefendants, "Plans"), "Payments"); frmPayment frm = new frmPayment(ref bindingPayments); if (!frm.IsDisposed) { frm.ShowDialog(this); bindingDefendants.ResetCurrentItem(); } } /******************************************************************************** * Remove/Archive - doesn't delete from the database ********************************************************************************/ else if (sender.Equals(mnuDefendantRemove)) { if (DialogResult.OK == MyMessageBox.Show(this, "Remove Defendant", MyDisplayMessage.Archive)) { this.SuspendLayout(); defendant.Reset(); defendant.RaiseChangedEvents = false; defendant.Active = false; defendant.Save(true); // set state to new to fake out the collection into removing it but not removing it from the db. //defendant.MyState = MyObjectState.New; defendant.RaiseChangedEvents = true; try { bindingDefendants.Remove(defendant); bindingDefendants.ResetBindings(true); this.ucFilter.Focus(); } catch (MyException ex) { MyMessageBox.Show(this, "Defendant", MyDisplayMessage.RemoveError, ex); } this.ResumeLayout(); } } }