private void butPrint_Click(object sender, System.EventArgs e) { #if DISABLE_MICROSOFT_OFFICE MessageBox.Show(this, "This version of Open Dental does not support Microsoft Word."); return; #endif if (listLetters.SelectedIndex == -1) { MsgBox.Show(this, "Please select a letter first."); return; } LetterMerge letterCur = ListForCat[listLetters.SelectedIndex]; letterCur.ImageFolder = comboImageCategory.SelectedTag <Def>().DefNum; string templateFile = ODFileUtils.CombinePaths(PrefC.GetString(PrefName.LetterMergePath), letterCur.TemplateName); string dataFile = ODFileUtils.CombinePaths(PrefC.GetString(PrefName.LetterMergePath), letterCur.DataFileName); if (!File.Exists(templateFile)) { MsgBox.Show(this, "Template file does not exist."); return; } PrintDocument pd = new PrintDocument(); if (!PrinterL.SetPrinter(pd, PrintSituation.Default, PatCur.PatNum, "Letter merge " + letterCur.Description + " printed")) { return; } if (!CreateDataFile(dataFile, letterCur)) { return; } Word.MailMerge wrdMailMerge; //Create an instance of Word. Word.Application WrdApp; try { WrdApp = LetterMerges.WordApp; } catch { MsgBox.Show(this, "Error. Is MS Word installed?"); return; } //Open a document. Object oName = templateFile; wrdDoc = WrdApp.Documents.Open(ref oName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); wrdDoc.Select(); wrdMailMerge = wrdDoc.MailMerge; //Attach the data file. wrdDoc.MailMerge.OpenDataSource(dataFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter; //WrdApp.ActivePrinter=pd.PrinterSettings.PrinterName; //replaced with following 4 lines due to MS bug that changes computer default printer object oWBasic = WrdApp.WordBasic; object[] oWBValues = new object[] { pd.PrinterSettings.PrinterName, 1 }; String[] sWBNames = new String[] { "Printer", "DoNotSetAsSysDefault" }; oWBasic.GetType().InvokeMember("FilePrintSetup", BindingFlags.InvokeMethod, null, oWBasic, oWBValues, null, null, sWBNames); wrdMailMerge.Execute(ref oFalse); if (letterCur.ImageFolder != 0) //if image folder exist for this letter, save to AtoZ folder { try { wrdDoc.Select(); wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument; wrdMailMerge.Execute(ref oFalse); WrdApp.Activate(); string tempFilePath = ODFileUtils.CreateRandomFile(Path.GetTempPath(), GetFileExtensionForWordDoc(templateFile)); Object oFileName = tempFilePath; WrdApp.ActiveDocument.SaveAs(oFileName); //save the document WrdApp.ActiveDocument.Close(); SaveToImageFolder(tempFilePath, letterCur); } catch (Exception ex) { FriendlyException.Show(Lan.g(this, "Error saving file to the Image module:") + "\r\n" + ex.Message, ex); } } //Close the original form document since just one record. wrdDoc.Saved = true; wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing); //At this point, Word remains open with no documents. WrdApp.WindowState = Word.WdWindowState.wdWindowStateMinimize; wrdMailMerge = null; wrdDoc = null; Commlog CommlogCur = new Commlog(); CommlogCur.CommDateTime = DateTime.Now; CommlogCur.CommType = Commlogs.GetTypeAuto(CommItemTypeAuto.MISC); CommlogCur.Mode_ = CommItemMode.Mail; CommlogCur.SentOrReceived = CommSentOrReceived.Sent; CommlogCur.PatNum = PatCur.PatNum; CommlogCur.Note = "Letter sent: " + letterCur.Description + ". "; CommlogCur.UserNum = Security.CurUser.UserNum; Commlogs.Insert(CommlogCur); DialogResult = DialogResult.OK; }
private void butPreview_Click(object sender, System.EventArgs e) { #if !DISABLE_MICROSOFT_OFFICE if (listLetters.SelectedIndex == -1) { MsgBox.Show(this, "Please select a letter first."); return; } LetterMerge letterCur = ListForCat[listLetters.SelectedIndex]; letterCur.ImageFolder = comboImageCategory.SelectedTag <Def>().DefNum; string templateFile = ODFileUtils.CombinePaths(PrefC.GetString(PrefName.LetterMergePath), letterCur.TemplateName); string dataFile = ODFileUtils.CombinePaths(PrefC.GetString(PrefName.LetterMergePath), letterCur.DataFileName); if (!File.Exists(templateFile)) { MsgBox.Show(this, "Template file does not exist."); return; } if (!CreateDataFile(dataFile, letterCur)) { return; } Word.MailMerge wrdMailMerge; //Create an instance of Word. Word.Application WrdApp; try{ WrdApp = LetterMerges.WordApp; } catch { MsgBox.Show(this, "Error. Is Word installed?"); return; } string errorMessage = ""; //Open a document. try { Object oName = templateFile; wrdDoc = WrdApp.Documents.Open(ref oName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); wrdDoc.Select(); } catch (Exception ex) { errorMessage = Lan.g(this, "Error opening document:") + "\r\n" + ex.Message; MessageBox.Show(errorMessage); return; } //Attach the data file. try { wrdMailMerge = wrdDoc.MailMerge; wrdDoc.MailMerge.OpenDataSource(dataFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument; wrdMailMerge.Execute(ref oFalse); } catch (Exception ex) { errorMessage = Lan.g(this, "Error attaching data file:") + "\r\n" + ex.Message; MessageBox.Show(errorMessage); return; } if (letterCur.ImageFolder != 0) //if image folder exist for this letter, save to AtoZ folder //Open document from the atoz folder. { try { WrdApp.Activate(); string tempFilePath = ODFileUtils.CreateRandomFile(Path.GetTempPath(), GetFileExtensionForWordDoc(templateFile)); Object oFileName = tempFilePath; WrdApp.ActiveDocument.SaveAs(oFileName); //save the document to temp location Document doc = SaveToImageFolder(tempFilePath, letterCur); string patFolder = ImageStore.GetPatientFolder(PatCur, ImageStore.GetPreferredAtoZpath()); string fileName = ImageStore.GetFilePath(doc, patFolder); if (!FileAtoZ.Exists(fileName)) { throw new ApplicationException(Lans.g("LetterMerge", "Error opening document" + " " + doc.FileName)); } FileAtoZ.StartProcess(fileName); WrdApp.ActiveDocument.Close(); //Necessary since we created an extra document try { File.Delete(tempFilePath); //Clean up the temp file } catch (Exception ex) { ex.DoNothing(); } } catch (Exception ex) { FriendlyException.Show(Lan.g(this, "Error saving file to the Image module:") + "\r\n" + ex.Message, ex); } } //Close the original form document since just one record. try { wrdDoc.Saved = true; wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing); } catch (Exception ex) { errorMessage = Lan.g(this, "Error closing document:") + "\r\n" + ex.Message; MessageBox.Show(errorMessage); return; } //At this point, Word remains open with just one new document. try { WrdApp.Activate(); if (WrdApp.WindowState == Word.WdWindowState.wdWindowStateMinimize) { WrdApp.WindowState = Word.WdWindowState.wdWindowStateMaximize; } } catch (Exception ex) { errorMessage = Lan.g(this, "Error showing Microsoft Word:") + "\r\n" + ex.Message; MessageBox.Show(errorMessage); return; } wrdMailMerge = null; wrdDoc = null; Commlog CommlogCur = new Commlog(); CommlogCur.CommDateTime = DateTime.Now; CommlogCur.CommType = Commlogs.GetTypeAuto(CommItemTypeAuto.MISC); CommlogCur.Mode_ = CommItemMode.Mail; CommlogCur.SentOrReceived = CommSentOrReceived.Sent; CommlogCur.PatNum = PatCur.PatNum; CommlogCur.Note = "Letter sent: " + letterCur.Description + ". "; CommlogCur.UserNum = Security.CurUser.UserNum; Commlogs.Insert(CommlogCur); #else MessageBox.Show(this, "This version of Open Dental does not support Microsoft Word."); #endif //this window now closes regardless of whether the user saved the comm item. DialogResult = DialogResult.OK; }
private void butPreview_Click(object sender, System.EventArgs e) { #if !DISABLE_MICROSOFT_OFFICE if (listLetters.SelectedIndex == -1) { MsgBox.Show(this, "Please select a letter first."); return; } LetterMerge letterCur = ListForCat[listLetters.SelectedIndex]; string templateFile = ODFileUtils.CombinePaths(PrefC.GetString(PrefName.LetterMergePath), letterCur.TemplateName); string dataFile = ODFileUtils.CombinePaths(PrefC.GetString(PrefName.LetterMergePath), letterCur.DataFileName); if (!File.Exists(templateFile)) { MsgBox.Show(this, "Template file does not exist."); return; } if (!CreateDataFile(dataFile, letterCur)) { return; } Word.MailMerge wrdMailMerge; //Create an instance of Word. Word.Application WrdApp; try{ WrdApp = LetterMerges.WordApp; } catch { MsgBox.Show(this, "Error. Is Word installed?"); return; } //Open a document. Object oName = templateFile; wrdDoc = WrdApp.Documents.Open(ref oName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); wrdDoc.Select(); wrdMailMerge = wrdDoc.MailMerge; //Attach the data file. wrdDoc.MailMerge.OpenDataSource(dataFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument; wrdMailMerge.Execute(ref oFalse); //Close the original form document since just one record. wrdDoc.Saved = true; wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing); //At this point, Word remains open with just one new document. WrdApp.Activate(); if (WrdApp.WindowState == Word.WdWindowState.wdWindowStateMinimize) { WrdApp.WindowState = Word.WdWindowState.wdWindowStateMaximize; } wrdMailMerge = null; wrdDoc = null; Commlog CommlogCur = new Commlog(); CommlogCur.CommDateTime = DateTime.Now; CommlogCur.CommType = Commlogs.GetTypeAuto(CommItemTypeAuto.MISC); CommlogCur.Mode_ = CommItemMode.Mail; CommlogCur.SentOrReceived = CommSentOrReceived.Sent; CommlogCur.PatNum = PatCur.PatNum; CommlogCur.Note = "Letter sent: " + letterCur.Description + ". "; CommlogCur.UserNum = Security.CurUser.UserNum; Commlogs.Insert(CommlogCur); #else MessageBox.Show(this, "This version of Open Dental does not support Microsoft Word."); #endif //this window now closes regardless of whether the user saved the comm item. DialogResult = DialogResult.OK; }