How to use the same drop down fields

P

Petra van Vuurem

How do I use the same drop down fields in multiple forms without recreating
the values with each form?
 
J

Jay Freedman

On Sun, 15 Nov 2009 08:51:01 -0800, Petra van Vuurem <Petra van
How do I use the same drop down fields in multiple forms without recreating
the values with each form?

The only way to do that is to prepare a macro that populates the list.
Here's some sample code (see
http://www.gmayor.com/installing_macro.htm if needed):

Sub PopulateCityDropdown()
Dim FieldName As String
Dim WasProtected As Boolean
WasProtected = False

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
WasProtected = True
End If

If Selection.FormFields.Count > 0 Then
FieldName = Selection.FormFields(1).Name
Else
FieldName = InputBox("Enter name of dropdown to fill:")
Do While Not ActiveDocument.Bookmarks.Exists(FieldName)
If StrPtr(FieldName) = 0 Then Exit Sub ' canceled
FieldName = InputBox("Enter name of dropdown to fill:")
Loop
End If

With ActiveDocument.FormFields(FieldName)
If .Type = wdFieldFormDropDown Then
.DropDown.ListEntries.Clear
.DropDown.ListEntries.Add "Akron"
.DropDown.ListEntries.Add "Boston"
.DropDown.ListEntries.Add "Chattanooga"
.DropDown.ListEntries.Add "Denver"
Else
MsgBox FieldName & " is not a dropdown."
Exit Sub
End If
End With

If WasProtected Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields,
NoReset:=True
End If
End Sub

You'd need a different macro for each list, or else this macro would
have to be modified to recognize which list goes with which field
name.

The macro can be assigned to a shortcut key and/or a toolbar button.
 
G

Greg Maxey

Another macro option is to create a bank of frequently used DDs in a
separate document (e.g., C:\FieldBank). You could then copy and paste these
fields into a current document using:

Sub CopyFromDDBank()
Dim oDoc1 As Word.Document
Dim oDoc2 As Word.Document
Dim pStr
pStr = InputBox("Enter field name to copy")
If StrPtr(pStr) = 0 Then Exit Sub
Set oDoc1 = ThisDocument
Set oDoc2 = Documents.Open(FileName:="C:\FieldBank", Visible:=False)
oDoc1.Activate
On Error GoTo Err_Handler
'Copy from bank
oDoc2.FormFields(pStr).Range.Copy
'Paste at IP in current document
Selection.Paste
Err_ReEntry:
oDoc2.Close
End
Err_Handler:
If Err.Number = 5941 Then
MsgBox "A field by this name does not exist in the DDBank"
End If
Resume Err_ReEntry
End Sub
 
M

macropod

Hi Petra,

If you only want to replicate the contents of the source document's formfields in the target documents, you can use INCLUDETEXT
fields in the target documents. No macros required. See Word's Help file for details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top