PDA

View Full Version : [SLEEPER:] Auto Fill Legacy Form Fields using Checkbox



TiffanyW
02-25-2019, 10:18 AM
I would like to add a checkbox that will auto field form fields once it is checked. I have a word 2010 document where the user needs to fill in the physical address of a company, each field is a different form field. Example: Street: Text1, Province: Text2, Country: Text3, etc. Below I wish to have a Legal Address section with a statement that says "Same as physical address" with a check box. Once the box is checked I would like to have the values from the physical address form fields copied to the Legal Address fields. I need to keep the fields as legacy form fields instead of the newer content controls as I have a macro that transmits the values into excel using .FormFields function.

Thanks for the help.

gmaxey
02-25-2019, 01:56 PM
Run a macro something like this on exit from the checkbox:


Sub FillLA()
ActiveDocument.Unprotect
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.FormFields("LegalAddress").Result = ActiveDocument.FormFields("Text1").Result & vbCr _
& ActiveDocument.FormFields("Text2").Result
Else
ActiveDocument.FormFields("LegalAddress").Result = vbNullString
End If
ActiveDocument.Protect wdAllowOnlyFormFields, True
End Sub

TiffanyW
02-26-2019, 12:37 PM
Thank you! How would I alter this to include a drop down list entry? I add to change to change a form field to a Content Control Drop Down List and need to have the selection from DropDown1 copied to DropDown2. I have tried changing the .FormFields to .ContentCotnrol but it still doesn't seem to work with .Results. I have also tried .ContentControl("Drop1").DropDown.ListEntries which also gives me an error.

Thanks

gmaxey
02-26-2019, 08:07 PM
I don't exactly follow what you want to do. The displayed value of a content control can be returned like this e.g.,:


Msgbox ActiveDocument.SelectContentControlByTitle("State").Item(1).Range.Text

TiffanyW
03-19-2019, 09:31 AM
Sorry for the delayed reply, I had to put my project on hold.
I had to include a Content Control Drop Down List in the address to restrict the selection. Therefore, now when the user selects the checkbox I want the selection from DropDownList1 to appear as the selection in DropDownList2. Does this make more sense?

gmaxey
03-19-2019, 12:49 PM
If you are going to use a content control checkbox then all the controls might as well be content controls. Following assumes the controls are named checkbox, dropdown1 and dropdown2. Code goes in the ThisDocument module:


Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Select Case ContentControl.Title
Case "Checkbox"
If ContentControl.Checked Then
With ActiveDocument.SelectContentControlsByTitle("Dropdown2").Item(1)
.Type = wdContentControlText
.Range.Text = ActiveDocument.SelectContentControlsByTitle("Dropdown1").Item(1).Range.Text
.Type = wdContentControlDropdownList
End With
End If
End Select
lbl_Exit:
Exit Sub
End Sub