Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add an apostrophe where required to a textfield

Status
Not open for further replies.

m3rlin

Programmer
Aug 31, 2003
11
AU
Hello,

I am trying to work out the best way to create a word template for use in a side business. This document is an agreement that gets sent to clients, and it's pretty generic except for the clients' name.

Is there a way that I can get the text that's entered into the textfield and then manipulate it? The textfield I'm talking about is the kind described here:
If I can use the exit macro to work out if the last character in the entered text is a s or not, I can apply the apostrophe correctly (I know that I could just do this with the entered text, but I can't rely on everyone doing that correctly..)

And also, any ideas how I'd go about running a macro to cycle through each of the textfields and replacing them with a variable entered into an inputbox?

Thanks in advance!
 
The textfields mentioned are FormFields, and are items in the the FormFields collection.

You have two questions.

RE: the last character. You do not fully give the logic you wish to apply, but the following may help get you going.
Code:
Sub LastChar()
Dim strIn As String
strIn = ActiveDocument.FormFields("Text1").Result
If Right(strIn, 1) = "s" Then
      strIn = Left(strIn, Len(strIn) - 1) & "new"
      ActiveDocument.FormFields("Text1").Result = strIn
    End If
End Sub
This checks the last character of the formfield Text1. If it is "s", then it strips off the "s" and replaces it with "new"

Input: "Bobs"
Output: "Bobnew"

You can of course do anything you want, logically, with the input string (the original Result), and the output string (final Result). This is fired with the OnExit macro of the formfield.
And also, any ideas how I'd go about running a macro to cycle through each of the textfields and replacing them with a variable entered into an inputbox?
Again, formfields make up the FormFields collection.
Code:
Sub AllFF()
Dim oFF As Word.FormField
Dim strVar As String
strVar = InputBox("Enter some text.")
   For Each oFF In ActiveDocument.FormFields()
        If oFF.Type = wdFieldFormTextInput Then
            oFF.Result = strVar
        End If
    Next
End Sub
You do not state fully what the logic is. However, again, this may help get you going. All text formfield results are replaced with the variable entered into the input box. This does not seem reasonable to me. Why would you replace all of them with a variable?

On the other hand if you wanted to do each one separately...
Code:
Sub AllFF()
Dim oFF As Word.FormField
Dim strVar As String
   For Each oFF In ActiveDocument.FormFields()
        If oFF.Type = wdFieldFormTextInput Then
           strVar = InputBox("Enter some text.")
            oFF.Result = strVar
        End If
    Next
End Sub
will do that. However...why on earth would you want this? It adds a step. Text formfields are for user input. In this case the user does the input via a inputbox, THEN it goes into the formfield. The user can always edit the formfield after.

Not sure I am getting the use of the inputbox.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top