Programming Word from .NET
Pages: 1, 2, 3, 4, 5, 6, 7
Creating the Visit Form
The final part of the three-part minuet is to create the Visit form from within the larger .NET application. A "real" Visit form might include a lot of data, and sophisticated search tools to pick the patient, the new diagnosis, and the appropriate medications. To keep things simple, we'll just use drop-downs to identify the patient and the new diagnosis, as shown in Figure 5.

Figure 5. New visit
Once the patient name and the new diagnosis have been added, new medications can be added as shown in Figure 6.

Figure 6. Add Rx.
That done, specific notes for the visit may be added, as shown in Figure 7.

Figure 7. Add notes.
Clicking Discard will discard all these changes (and the new diagnosis and medications for the user will not be recorded). You might want to pop up an "Are you sure?" dialog!
Clicking Save will save the new visit, the new diagnosis, and any new prescriptions to the database, and will create the new personalized Word document for mailing to the patient. Let's examine the Save code in some detail. Here it is in full (analysis follows):
private void btnSave_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
// gather data
int patientID = Convert.ToInt32(patientsComboBox.SelectedValue);
string patientName =
((System.Data.DataRowView)(patientsComboBox.SelectedItem)).Row.ItemArray[1].ToString();
int diagnosisID = Convert.ToInt32(diagnosesComboBox.SelectedValue);
string diagnosisString =
((System.Data.DataRowView)(diagnosesComboBox.SelectedItem)).Row.ItemArray[1].ToString();
//** update the database **
// update the visit table
this.visitsTableAdapter.InsertVisit(visitTime, txtNotes.Text, patientID);
// update the Diagnosis table
this.patientToDiagnosesTableAdapter.InsertPatientToDiagnosis(patientID, diagnosisID);
// add the new meds
string newMeds = string.Empty;
foreach (MedicationHolder holder in lbMeds.Items)
{
newMeds += holder.MedName + ", ";
int numAdded = this.patientToMedicationTableAdapter.InsertNewPatientMedication(
patientID, holder.MedID, visitTime);
if (numAdded != 1)
{
MessageBox.Show("Unable to add " + holder.MedName, "Uh oh",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
this.patientToMedicationTableAdapter.InsertNewPatientMedication(
patientID, holder.MedID, visitTime);
}
}
// ** Generate the letter **
// Get the application object
Microsoft.Office.Interop.Word .application wordApp =
new Microsoft.Office.Interop.Word .application();
// Get the document object
object fileName = @"C:\Temp\Patient Letter.doc";
object confirmConversions = Type.Missing;
object readOnly = Type.Missing;
object addToRecentFiles = Type.Missing;
object passwordDoc = Type.Missing;
object passwordTemplate = Type.Missing;
object revert = Type.Missing;
object writepwdoc = Type.Missing;
object writepwTemplate = Type.Missing;
object format = Type.Missing;
object encoding = Type.Missing;
object visible = Type.Missing;
object openRepair = Type.Missing;
object docDirection = Type.Missing;
object notEncoding = Type.Missing;
object xmlTransform = Type.Missing;
Microsoft.Office.Interop.Word .document doc = wordApp.Documents.Open(
ref fileName,
ref confirmConversions, ref readOnly, ref addToRecentFiles,
ref passwordDoc, ref passwordTemplate, ref revert, ref writepwdoc,
ref writepwTemplate, ref format, ref encoding, ref visible, ref openRepair,
ref docDirection, ref notEncoding, ref xmlTransform);
// replace book marks
ReplaceBookmarkText(doc, "PatientName", patientName);
ReplaceBookmarkText(doc, "Diagnosis", diagnosisString);
ReplaceBookmarkText(doc, "PatientAddress", patientName);
if ( txtNotes.Text.Length > 0 )
ReplaceBookmarkText(doc, "notes", "Notes:\n" + txtNotes.Text);
List<string> existingMedications = GetExistingMedications(patientID);
string existingMeds = string.Empty;
foreach ( string med in existingMedications )
{
existingMeds += med + ", ";
}
ReplaceBookmarkText(doc, "ExistingMedications", existingMeds);
ReplaceBookmarkText(doc, "NewMedications", newMeds);
// save the new document
object newFileName = @"C:\Temp\" + patientName + ".doc";
object fileFormat = Type.Missing;
object lockComments = Type.Missing;
object WritePW = Type.Missing;
object readOnlyRecommended = Type.Missing;
object embedTrueTypeFonts = Type.Missing;
object saveNativePictuormat = Type.Missing;
object saveFormsData = Type.Missing;
object saveAsAOCELetter = Type.Missing;
object insertLineBreaks = Type.Missing;
object allowSubstittions = Type.Missing;
object lineEnding = Type.Missing;
object AddBiDiMarks = Type.Missing;
object refWritePW = Type.Missing;
object saveNativePictureFormat = Type.Missing;
doc.SaveAs(ref newFileName , ref fileFormat, ref lockComments, ref passwordDoc,
ref addToRecentFiles, ref WritePW, ref readOnlyRecommended, ref embedTrueTypeFonts,
ref saveNativePictureFormat, ref saveFormsData, ref saveAsAOCELetter, ref encoding,
ref insertLineBreaks, ref allowSubstittions, ref lineEnding, ref AddBiDiMarks);
// close word
object saveChanges = Type.Missing;
object originalFormat = Type.Missing;
object routeDocument = Type.Missing;
wordApp.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
Cursor = Cursors.Default;
this.Close(); // close the dialog
}

