PDA

View Full Version : [SOLVED:] Run generic search/replace macro on selected text form fields?



clhare
12-31-2013, 10:07 AM
I have a document that contains multiple text form fields. In some of those fields, users will type in dates that are spelled out (ex., January 1, 2014). For only those specific fields, I would like to also run an exit macro that will replace any spaces with a required space so the dates do not split. I just can't figure out how to set up the macro. Also, does the field have to be bookmarked or can the macro update the field using it's index number? Ideally, I would like to have one macro that can be used as an exit macro by any of these text form fields.

Thanks!

Cheryl

gmaxey
12-31-2013, 10:42 AM
Sub ReplaceSpacesInDate()
Dim oFld As Word.FormField
If Selection.FormFields.Count = 1 Then
Set oFld = ActiveDocument.FormFields(Selection.FormFields(1).Name)
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
Set oFld = ActiveDocument.FormFields(Selection.Bookmarks(Selection.Bookmarks.Count).Na me)
End If
oFld.Result = Replace(oFld.Result, Chr(32), Chr(160))
End Sub

clhare
12-31-2013, 11:55 AM
That works perfect! Thank you so much! What does the If statement do? Does it add a bookmark to the field if it doesn't have one already?

Cheryl

gmaxey
12-31-2013, 01:38 PM
Cheryl,

It is just the way it is: http://word.mvps.org/FAQs/TblsFldsFms/GetCurFmFldName.htm