Can one do a find/replace within a drop-down on a form?

D

dthornbery

We've created a form with a drop-down for names. One of those names has
changed. Can we do a find & replace across the completed forms substituting
the new name for the one that appeared in the drop-down?
 
G

Graham Mayor

Not with find and replace, but you can do it with a macro.

The following will remove an entry from the same dropdown list in a
collection of similar forms placed in a separate folder for that purpose
(test with COPIES!!!). The macro prompts for the folder containing the
documents.

If the old entry is the selected entry in the completed form, then the new
entry will be the selected entry after the macro is run. If the old entry if
not the selected entry, the old entry will simply be removed from the list
and the new entry added.

You will need to change the following lines in the macro to reflect the
information relating to your dropdown field

ddName = "Dropdown1"
The bookmark name of the dropdown field

oldEntry = "Name1"
The entry to be changed *EXACTLY* as it appears in the list

oEPos = 1
The position in the list of the old entry (here the first item in the list)

newEntry = "Name2"
The name you wish to appear as a replacement entry

nEPos = 3
The total number of entries in list. The replacement entry will be added to
the bottom of the list and will be used to check whether the document has
already been processed - so if you run the macro again on the same document,
you won't add the new entry again.

If necessary, see http://www.gmayor.com/installing_macro.htm

Sub ChangeDDEntry()
Dim strFile As String
Dim strPath As String
Dim oDoc As Document
Dim iFld As Integer
Dim oDD As DropDown
Dim ddName As String
Dim oldEntry As String
Dim newEntry As String
Dim oEPos As Integer
Dim nEPos As Integer
Dim fDialog As FileDialog

ddName = "Dropdown1" 'name of dropdown field
oldEntry = "Name1" 'entry to be changed
oEPos = 1 'position in list of old entry
newEntry = "Name2" 'replacement entry
nEPos = 3 'number of entries in list

Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select Folder containing the documents and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With
WordBasic.DisableAutoMacros 1
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
strFile = Dir$(strPath & "*.do?")
While strFile <> ""
Set oDoc = Documents.Open(strPath & strFile)
Set oDD = oDoc.FormFields(ddName).DropDown
If oDoc.FormFields(ddName).Result = oldEntry Then
oDoc.FormFields(ddName).Result = newEntry
End If
On Error Resume Next
sNum = oDD.ListEntries(oldEntry).Index
If sNum = oEPos Then 'This is the position in the list of the old entry
oDD.ListEntries(oEPos).Delete
End If
sNum = oDD.ListEntries(newEntry).Index
If sNum <> nEPos Then 'This is the last entry position
oDD.ListEntries.Add newEntry
End If

oDoc.Close SaveChanges:=wdSaveChanges
strFile = Dir$()
Wend
WordBasic.DisableAutoMacros 0
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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