PDA

View Full Version : Replace spaces with required spaces in form fields?



clhare
11-16-2007, 07:33 AM
I want to create a macro that I can assign to specific text form fields in a document (not on a userform) as an exit macro that will replace the spaces in the user's text with required spaces once the user tabs out of the field.

I created the following macro and set it as the exit macro for a particular form field in the document, but it doesn't work. When I tab out of the field, I get a message that the requested member of the collection does not exist.

Sub ChangeText()

Dim strFFText As String
Dim i As Integer

' Assign form field text to a string
strFFText = ActiveDocument.FormFields(i).Result

' Replace the spaces in the string with required spaces
strFFText = Replace(strFFText, Chr$(32), Chr$(160))

' Assign the new string back to the form field
ActiveDocument.FormFields(i).Result = strFFText

End Sub

What am I doing wrong? The error occurs in the line where I assign the result of the form field to a string, but I can't figure out any other way to do it.

fumei
11-16-2007, 07:47 AM
For one thing...i = 0

so ActiveDocument.Formfields(0).Result will, yup, return a not exist error. As...it doesn't.

clhare
11-16-2007, 08:30 AM
That's what I was thinking, so how do I reference the "selected" form field in a document? I want the macro to be an exit macro for several form fields in the document.

OTWarrior
11-16-2007, 09:19 AM
on entry to the formfield, get the code to change a public variable to a string or number, and call that string or number into your code.

example (using the string method, which is only good if you have named all your formfields with bookmark names):


option explicit
public strFFName as string

sub Entrymacro()
If Selection.FormFields.Count = 1 Then
'No textbox but a check- or listbox
strFFName= Selection.FormFields(1).Name
ResultCheck = ActiveDocument.FormFields(strFFName).Result

Exit Sub
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
strFFName= Selection.Bookmarks(Selection.Bookmarks.Count).Name
ResultCheck = ActiveDocument.FormFields(strFFName).Result
Exit Sub
end if


hope that helps

clhare
11-16-2007, 09:45 AM
Why do you need the two parts of the IF statement? I don't understand what the count is for.

fumei
11-16-2007, 10:23 AM
Frankly, unless you have a good reason to do so, I would not do it individually. It is such a pain referencing the current formfield. Why not do it globally?Sub CleanSpaces()
Dim DocFF As FormFields
Dim oFF As FormField
Set DocFF = ActiveDocument.FormFields
For Each oFF In DocFF
If oFF.Type = wdFieldFormTextInput Then
oFF.Result = Replace(oFF.Result, Chr$(32), Chr$(160))
End If
Next
End Subactions the Replace in all text formfields.

I wonder about your Replace instruction though.

But anyway, if you want, as you seem to indicate, to action only specific text formfields, then you could make an array of those. Like this:Sub CleanSpaces2()
Dim JustThese()
Dim DocFF As FormFields
Dim oFF As FormField
Dim var

JustThese = Array("Text1", "Text2")
Set DocFF = ActiveDocument.FormFields
For Each oFF In DocFF
For var = 0 To UBound(JustThese)
If oFF.Name = JustThese(var) Then
oFF.Result = Replace(oFF.Result, Chr$(32), _
Chr$(160))
Exit For
End If
Next
Next
Set DocFF = Nothing
End SubNow only Text1 and Text2 would get actioned.

The reason I suggest this is, say you have 10 formfields. That means, at minimum 10 separate procedure executions. And, if you do the both OnEntry and OnExit route, 20 separate procedure executions.

Doing it globally means ONE procedure execution. Also, there is no use of Selection.

This is a design issue. What is the purpose?

The purpose is to do a Replace on all (or specific) text formfields.

Then do that.

From a design point of view, the purpose is NOT to execute a procedure on individual formfields. It is to execute a procedure on multiple formfields.

So, do that.

clhare
11-16-2007, 10:28 AM
Thank you! Either way will be very helpful to me.

fumei
11-16-2007, 10:32 AM
Oh, and notice that in the specific formfields example, there is no test for formfield Type. Presumably you would not make the array include formfields you do not want to action.