Wade, this is much more challenging than your last question. There are two difficult questions. The first is how to have your form, itself, change based on whether or not a particular answer is picked in a drop down. The second is how to reference a formfield that may or maynot exist.
Let me deal with the first question. What follows is code that I have used for more than 10 years with a legacy form. It inserts a formfield at a bookmark if a checkbox is checked. You should be able to adapt it to a particular drop-down choice.
Note this was written for (and works with) legacy form fields in a form protected for filling in forms with a password.
If you are selling this, write to discuss a commission.
Code:
Public sPass
' Module and Project Written by Charles Kyle Kenyon
' May 2001, Copyright 2001 All rights reserved
Sub AutoNew()
AutoOpen
End Sub
Sub AutoOpen()
SetDocumentVariablePassword
sPass = ActiveDocument.Variables("FormPassWord")
LockDocForForms 'Lock this form so formfields will work
GoToStartingField 'If set for particular case, goto witness info
End Sub
Private Sub LockDocForForms()
'Locks Document or Template for Forms upon opening or creation
If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=sPass
End If
End Sub
Private Sub GoToStartingField()
' Checks to see if form has been modified for a particular _
defendant. If it has, then start with Witness name, _
otherwise, start with County name
If ActiveDocument.FormFields("DefName").Result = "DEFENDANT'S NAME?" Then
ActiveDocument.FormFields("County").Select
Else
ActiveDocument.FormFields("WitName").Select
End If
End Sub
Private Sub SetDocumentVariablePassword()
Dim num As Integer, aVar As Variable
For Each aVar In ActiveDocument.Variables
If aVar.Name = "FormPassWord" Then num = aVar.Index 'End If
Next aVar
If num = 0 Then
ActiveDocument.Variables.Add Name:="FormPassWord", Value:="GF126"
Else
ActiveDocument.Variables(num).Value = "GF126"
End If
End Sub
Public Sub ProtectSubpoenaForForms()
On Error GoTo NotSubpoena
With ActiveDocument
sPass = .Variables("FormPassWord") 'will trip error message if not set
If .ProtectionType = wdNoProtection Then .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=sPass
' End If
End With
On Error GoTo 0
End
NotSubpoena:
MsgBox "This is not a subpoena form based on the right template!", vbExclamation, "Oh! Oh! Wrong Document for this macro!"
End Sub
Sub DucesTecum()
'
' DucesTecum Macro
' OnExit macro for DucesTecum Checkbox
' "&chr(10)&"Macro written 05/16/2001 by Charles Kyle Kenyon
'
Dim strBringWith As String, rRange As Range
With ActiveDocument
UnProtectSubpoena 'subroutine below
Set rRange = .Bookmarks("YouBringYes").Range
'Save result of form field
strBringWith = .FormFields("BringWhat").Result
If .FormFields("chkDucesTecum").CheckBox.Value _
= True Then
.FormFields("DucesTecumTitle").TextInput.EditType _
Type:=wdRegularText, Default:=" Duces Tecum "
If strBringWith = "" Then strBringWith = _
"Bring what?" 'End If
.FormFields("BringWhat").TextInput.EditType _
Type:=wdRegularText, Default:=strBringWith, _
Enabled:=True
rRange.Font.DoubleStrikeThrough = False
rRange.Font.Bold = True
.FormFields("BringWhat").Select
Else
.FormFields("DucesTecumTitle").TextInput.EditType _
Type:=wdRegularText, Default:=" ", Enabled:=False
.FormFields("BringWhat").TextInput.EditType _
Type:=wdRegularText, Default:="", Enabled:=False
rRange.Font.DoubleStrikeThrough = True
rRange.Font.Bold = False
.FormFields("chkThirdParty").Select
End If
.Protect wdAllowOnlyFormFields, True, sPass
End With
End Sub
Sub ThirdParty()
'
' ThirdParty Macro
' OnExit Macro for Third-Party Checkbox
' "&chr(10)&"Macro written 05/16/2001 by Charles Kyle Kenyon
'
Dim rRange As Range
With ActiveDocument
UnProtectSubpoena 'subroutine below
Set rRange = .Bookmarks("ThirdPartyLanguage").Range
rRange.Font.DoubleStrikeThrough = _
Not .FormFields("chkThirdParty").CheckBox.Value
rRange.Font.Bold = _
.FormFields("chkThirdParty").CheckBox.Value
.Protect wdAllowOnlyFormFields, True, sPass
End With
End Sub
Sub UnprotectSubpoenaForm() 'For Menu
MsgBox "The password to unprotect this document is " _
& ActiveDocument.Variables("FormPassWord") & "." & vbCrLf _
& "You must use the same password if you reprotect it, otherwise the form will not work. Please write it down.", vbExclamation, "Subpoena Form Password Information"
UnProtectSubpoena
End Sub
Private Sub UnProtectSubpoena() 'Subroutine for other procedures
With ActiveDocument
sPass = .Variables("FormPassWord")
If .ProtectionType <> wdNoProtection Then .Unprotect (sPass)
' End If
End With
End Sub
Write back when you have finished this part and we'll talk about using an IF field to reference a formfield that may not exist.