PDA

View Full Version : Solved: Unable to populate form field



BostonVB
08-09-2011, 05:50 PM
I have a macro that someone gave me that isn’t working properly.

I have some experience in Excel and I’m not sure how to work with the fields in Word.

The piece that seems to not be working is the final line of code
ThisDocument.FormFields(149).Result = ReadmitRisk

I have tested the fields that determine the value for ReadmitRisk and when I check this in the Immediate window the calculations are working as appropriate. However, the goal is to get that value to populate in the text form field ReadmitRisk.

The following variables are being used to calculate the value for ReadmitRisk.
Admit Source = AdmitSource
Hosp. Visits Last 6 months = HospVisits
ED. Visits Last 6 months = EDVisits
Polypharmacy (≥5 routine meds)? = PolyPharm
Total # Problem Meds: = TotProblemMeds

This macro is set to run after exit of TotProblemMeds.

It also looks like there is some use of .select which I would never do in Excel. Is there a reason to do it in Word?

Test file attached.

Any help is much appreciated!

BostonVB
08-09-2011, 08:02 PM
ok, so the first part of my answer was actually an easy fix... I simply changed the line to:

ActiveDocument.FormFields("ReadmitRisk").Result = ReadmitRisk

I'm still trying to clean-up and dissect the rest of the code. Any thoughts or suggestions are very much welcomed!

Updated complete code:

Sub RecalcReadmitRisk()
Dim ReadmitRisk, AdmitSource, PolyPharm As String
Dim High, Moderate, Low As Integer
Dim HospVisits, EDVisits, TotProblemMeds As Variant
With Application
.ScreenUpdating = False
High = 0
Moderate = 0
Low = 0
AdmitSource = ActiveDocument.FormFields("AdmitSource").Result
Select Case AdmitSource
Case "ER"
High = High + 1
Case "Direct"
Moderate = Moderate + 1
Case "Scheduled"
Low = Low + 1
End Select
HospVisits = ActiveDocument.FormFields("HospVisits").Result
Select Case HospVisits
Case 3 To 30
High = High + 1
Case 1 To 2
Moderate = Moderate + 1
Case 0
Low = Low + 1
End Select
EDVisits = ActiveDocument.FormFields("EDVisits").Result
Select Case EDVisits
Case 3 To 30
High = High + 1
Case 1 To 2
Moderate = Moderate + 1
Case 0
Low = Low + 1
End Select
PolyPharm = ActiveDocument.FormFields("PolyPharm").Result
Select Case PolyPharm
Case "Y"
High = High + 1
Case "N"
Moderate = Moderate + 1
End Select
TotProblemMeds = ActiveDocument.FormFields("TotProblemMeds").Result
Select Case TotProblemMeds
Case 1 To 1000
High = High + 1
Case 0
Moderate = Moderate + 1
End Select
If High >= 3 Then ReadmitRisk = "High"
If Moderate >= 3 Then ReadmitRisk = "Moderate"
If Low >= 3 Then ReadmitRisk = "Low"
If High = 2 Then
ReadmitRisk = "High"
ElseIf Moderate = 2 Then
ReadmitRisk = "Moderate"
ElseIf Low = 2 Then
ReadmitRisk = "Low"
End If
ActiveDocument.FormFields("ReadmitRisk").Result = ReadmitRisk
.ScreenUpdating = True
End With
End Sub

Jay Freedman
08-10-2011, 07:38 PM
The code, now that you've fixed the "ThisDocument" goof, looks reasonable. My only comment in that respect is that the statements that turn .ScreenUpdating off and on are probably unnecessary.

I think there's a small hole in the logic, though. It would be possible, depending on the values entered in the five fields, to get High = 3 and Low = 2. Then the first batch of If statements would set ReadmitRisk to "High" (which is probably what the "ReadmitRisk" field should say), but then the final If ... ElseIf ... End If group will change ReadmitRisk to "Low". Is that really what you intend?

BostonVB
08-11-2011, 10:07 AM
Hi Jay,
That's a great catch and I was just discussing if that should change this morning. I agree it is a hole in the logic and I'm currently looking for more clarification on it.

Thanks for the catch!!