Waubain
02-25-2012, 08:58 AM
Sorry for the ramble. I am an end user and typically work in Access. A group of my pharmacist peers have asked if I can automate a form letter sent out to patients who are on a blood thinner, are having surgery soon and must be transitioned to a different medicaton. I made a template which creates a new document and opens a userform with information need to fill in the document.
The tables are giving me the biggest challenge. I attached a jpg of the two possible tables, based on if they need medication once or twice a day (There is more in the template I did not include for brevity). The MOST rows the table can have is 14 including the header. I included my very crude code I have put together. I also recorded a macro in Word just to see what is involved in formatting a table in VBA. Keep in mind that it took about 2 days to read and have a basic understanding what I have already. I left off the initialize sub.
I am looking for recommendations on how to proceed next:
1) Build the entire table, formatting and enter cell contents with vba. This option gives me the most flexibility on which table is inserted, but has the steepest learning curve since I cannot find a good example/tutorial on table building and formatting.
2) Prebuild a formatted table in the template with 14 rows and bookmark each cell and then delete the null rows at the end. This seems the easiest at this point, although I know I would still need to do some formatting of the cells. There is also the problem of two different forms. I would either need two different templates or have two tables one after the other and then delete the table not used
Any thoughts on how to proceed would be greatly appreciated.
Here is the code for the OK button on he userform
Private Sub cmdOK_Click() ' Makes all calculations fills in Document_1 when clicked
Dim strProDate As String
Dim strINRDate As String
Dim dtpWarfDate As Date
Dim strWarfDate As String
Dim intWarfDays As Integer
Dim strEnoxText As String
Dim iRows As Integer
Dim iCols As Integer
Dim tblSchedule As Table
Dim rRange As Range
Dim DosingTable As String
Dim intPreDays As Integer
Dim intPostDays As Integer
'calculates last date to take warfarin
intWarfDays = cboWarfDays * -1
dtpWarfDate = DateAdd("d", intWarfDays, DTPickerProcedure.Value)
strWarfDate = Format(dtpWarfDate, "dddd, mmmm d, yyyy")
'Formats Procedure and INR dates
strProDate = Format(DTPickerProcedure.Value, "dddd, mmmm d, yyyy")
strINRDate = Format(DTPickerINR.Value, "dddd, mmmm d, yyyy")
'creates enoxaparin string
If cboEnoxSig = "BID" Then
strEnoxText = "every morning before 11am and every evening after 5pm"
Else: strEnoxText = "every morning before 11am"
End If
Application.ScreenUpdating = False 'turns off bookmark updating'
'Calculates number of columns and rows for schedule table
intPreDays = cboPreDays.Value
intPostDays = cboPostDays.Value
iRows = intPreDays + intPostDays + 2
If cboEnoxSig = "BID" Then
iCols = 4
Else: iCols = 3
End If
' get bookmark range
Set rRange = ActiveDocument.Bookmarks("DosingTable").Range
'Inserts a table with iCols columns and iRows rows at DosingTable bookmark
Set tblSchedule = ActiveDocument.Tables.Add(Range:=rRange, NumRows:=iRows, NumColumns:=iCols, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed)
'Enters data into bookmarks
With ActiveDocument
.Bookmarks("DateofProcedure").Range.Text = strProDate
.Bookmarks("INRDate").Range.Text = strINRDate
.Bookmarks("LastWarfarinDoseDate").Range.Text = strWarfDate
.Bookmarks("EnoxaparinDose").Range.Text = cboEnoxDose.Value & " " & strEnoxText
.Bookmarks("PatientName").Range.Text = "for " & txtPatientName.Value
End With
Application.ScreenUpdating = True 'turns on bookmark updating
Unload Me 'unloads userform
End Sub
The tables are giving me the biggest challenge. I attached a jpg of the two possible tables, based on if they need medication once or twice a day (There is more in the template I did not include for brevity). The MOST rows the table can have is 14 including the header. I included my very crude code I have put together. I also recorded a macro in Word just to see what is involved in formatting a table in VBA. Keep in mind that it took about 2 days to read and have a basic understanding what I have already. I left off the initialize sub.
I am looking for recommendations on how to proceed next:
1) Build the entire table, formatting and enter cell contents with vba. This option gives me the most flexibility on which table is inserted, but has the steepest learning curve since I cannot find a good example/tutorial on table building and formatting.
2) Prebuild a formatted table in the template with 14 rows and bookmark each cell and then delete the null rows at the end. This seems the easiest at this point, although I know I would still need to do some formatting of the cells. There is also the problem of two different forms. I would either need two different templates or have two tables one after the other and then delete the table not used
Any thoughts on how to proceed would be greatly appreciated.
Here is the code for the OK button on he userform
Private Sub cmdOK_Click() ' Makes all calculations fills in Document_1 when clicked
Dim strProDate As String
Dim strINRDate As String
Dim dtpWarfDate As Date
Dim strWarfDate As String
Dim intWarfDays As Integer
Dim strEnoxText As String
Dim iRows As Integer
Dim iCols As Integer
Dim tblSchedule As Table
Dim rRange As Range
Dim DosingTable As String
Dim intPreDays As Integer
Dim intPostDays As Integer
'calculates last date to take warfarin
intWarfDays = cboWarfDays * -1
dtpWarfDate = DateAdd("d", intWarfDays, DTPickerProcedure.Value)
strWarfDate = Format(dtpWarfDate, "dddd, mmmm d, yyyy")
'Formats Procedure and INR dates
strProDate = Format(DTPickerProcedure.Value, "dddd, mmmm d, yyyy")
strINRDate = Format(DTPickerINR.Value, "dddd, mmmm d, yyyy")
'creates enoxaparin string
If cboEnoxSig = "BID" Then
strEnoxText = "every morning before 11am and every evening after 5pm"
Else: strEnoxText = "every morning before 11am"
End If
Application.ScreenUpdating = False 'turns off bookmark updating'
'Calculates number of columns and rows for schedule table
intPreDays = cboPreDays.Value
intPostDays = cboPostDays.Value
iRows = intPreDays + intPostDays + 2
If cboEnoxSig = "BID" Then
iCols = 4
Else: iCols = 3
End If
' get bookmark range
Set rRange = ActiveDocument.Bookmarks("DosingTable").Range
'Inserts a table with iCols columns and iRows rows at DosingTable bookmark
Set tblSchedule = ActiveDocument.Tables.Add(Range:=rRange, NumRows:=iRows, NumColumns:=iCols, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed)
'Enters data into bookmarks
With ActiveDocument
.Bookmarks("DateofProcedure").Range.Text = strProDate
.Bookmarks("INRDate").Range.Text = strINRDate
.Bookmarks("LastWarfarinDoseDate").Range.Text = strWarfDate
.Bookmarks("EnoxaparinDose").Range.Text = cboEnoxDose.Value & " " & strEnoxText
.Bookmarks("PatientName").Range.Text = "for " & txtPatientName.Value
End With
Application.ScreenUpdating = True 'turns on bookmark updating
Unload Me 'unloads userform
End Sub