#1
|
|||
|
|||
Macro to find text and replace with form field containing that text
Hi, I am in over my head here, and I am hoping that someone on this forum can help me out... I have a series of documents that were created with a mailmerge and contain several copies of the same form (using legacy forms because some of our computers are still running Word 2003/2004), but for different students' names and info at the top of each page (I am a teacher). Eventually, I hope to export the form data to Excel, but I need some of the info inserted in the mail merge to be converted to form fields. Here's what I am thinking the macro needs to do: 1. Find the string "Student #:" 2. Select the next word to the right (i.e. the student number) 3. Cut this word 4. Insert a text form field pre-populated with the cut text 5. Repeat for all instances in the document I would really appreciate any help than anyone can give me here. Thanks, Ian |
#2
|
||||
|
||||
Hi Ian,
Is the 'student number' numbers only? What characters are there between the 'Student #:' and the 'student number'?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Yes, numbers only. There is one space. It says: "Student #: 1234567" (no quotes). If it matters, the student number varies in length--some are six digits and some are seven.
|
#4
|
||||
|
||||
Hi Ian,
Try: Code:
Sub AddFormFields() Dim pState As Boolean, Pwd As String, StrTmp As String, FFld As FormField With ActiveDocument pState = False If .ProtectionType <> wdNoProtection Then Pwd = InputBox("Please enter the Password", "Password") pState = True .Unprotect Pwd End If With .Range With .Find .ClearFormatting .Text = "Student #: [0-9]@>" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = True .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found StrTmp = .Duplicate.Words.Last.Text Set FFld = ActiveDocument.FormFields.Add(Range:=.Duplicate.Words.Last, _ Type:=wdFieldFormTextInput) FFld.Result = StrTmp .Start = FFld.Range.End .Find.Execute Loop End With If pState = True Then .Protect Type:=wdAllowOnlyFormFields, Noreset:=True, _ Password:=Pwd pState = False End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Wow! Thanks! That did it. I really appreciate your help.
Ian |
#6
|
|||
|
|||
Hello. My task is similar except I need to replace the entire search string with a text form field and have the default text display only the entire matched string (all instances).
The fields that need to be removed and replaced by a text form field all look similar to this: < Insert ... here> .Text = "\< Insert *\>" works correctly at identifying all valid fields but the text form field is replacing only the last character of the matched string so the results all look like this: < Insert .... here<Insert .... here>. Can anyone suggest how to get rid of the leading text so the text form field default reads exactly as it was found? Any aditional help would be appreciated. Thanks, Harold |
#7
|
||||
|
||||
Hi Harold,
You're only getting the last word because that's what the macro was designed to do. You need to change: .Duplicate.Words.Last.Text to: .Duplicate.Text and change: .Duplicate.Words.Last to: .Duplicate
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Works perfectly. Thanks very much.
Harold |
#9
|
|||
|
|||
Variant: Macro to find text and replace with form field containing that text
Hi,
I was looking into how to "find text and replace with form field", and came across Paul's post. It occurred to me how many people he must have helped since his original posting of the macro. I find it inspiring! I am in way over my head trying to modify your original macro to suit my document. I have modified it, but it keeps freezing. I've attached the code I'm using below. Perhaps, if it's important, a difference is that the string is sometimes repeated throughout the document, and sometimes it will be different. Another caveat is that some of the text strings are already form fields but not in all instances. Where they are not form fields, whether they existed before (the macro created them) as form fields or not, I would want to replace them with the form field. So here's what I'm thinking the macro needs to do: 1. Find the first instance of a text string that matches the following format: "A-F2B" or "A-F2", without the quotes. Where A is a text value between A and Z; - is a hyphen character, F is J,F,M,A,S,N,D (the first letters of the months) 2 is any value between 1 and 31 (for the dates in the month) and any characters after such as B in the example are letters or numbers. 2. Cut this string (not sure if this would be conditional upon whether or not it is already a form field, or just do in all instances.) 3. Insert a text form field with the cut text so that the default text is the same as the originally found text. Though I'm not sure how the bookmark name would work (I would want it to be independent from the original text - so could be RefCode_* or any other suggested label.) 4. Replace all non-form field instances with the form field matching the non-form field string. 5. Repeat for all instances. I would really appreciate any help that anyone can give me here. Thanks a lot, Mark ---- this is the code I am currently working with: Sub AddFormFields() Dim pState As Boolean, Pwd As String, StrTmp As String, FFld As FormField With ActiveDocument pState = False If .ProtectionType <> wdNoProtection Then Pwd = InputBox("Please enter the Password", "Password") pState = True .Unprotect Pwd End If With .Range With .Find .ClearFormatting .Text = "[A-Z]-???" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = True .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found StrTmp = .Duplicate.Text Set FFld = ActiveDocument.FormFields.Add(Range:=.Duplicate, _ Type:=wdFieldFormTextInput) FFld.Result = StrTmp .Start = FFld.Range.End .Find.Execute Loop End With If pState = True Then .Protect Type:=wdAllowOnlyFormFields, Noreset:=True, _ Password:=Pwd pState = False End With End Sub |
#10
|
||||
|
||||
If this is a form, using formfields, and you want to replicate the data throughout the document, there is no need for a macro - all you need do is:
• ensure the formfield has an internal bookmark name • ensure the formfield's 'calculate on exit' property is checked • insert cross-references to the formfield's internal bookmark name wherever you want the data replicated.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Thanks Paul,
What a pleasant surprise that you responded. Given that the thread was a few years old, and showed 'solved' I wasn't sure if anyone would. The problem is that it isn't a form, it is a very long document, and many of the text strings throughout the document are not yet form fields. Since there are hundreds of instances of each string, it would take a long time to manually go through and replace them with the cross-references. So I was thinking about whether its possible to automatically create the form fields and populate the data throughout the document. |
#12
|
||||
|
||||
Turning an existing set of strings like "A-F2B" or "A-F2" into cross-references requires nothing more than a Find/Replace, making the conversion a once-off exercise. All you need for each of the strings you want to convert is:
a) a single formfield to which the cross-references can point; and b) a single cross-reference you can copy to the clipboard for the Find/Replace operation.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
I found this post through Google and I love it.
I do have a question that I can't resolve. My selected text contains 2 words, let say it's "My Kitchen" (without the quotationmarks) If I use this, the screen only blinks and Word freezes. What do I need to change to make it work with 2 words. When it works with 2 words, I want to replace the .Text field with a inputbox, but first I need tot get it to work with 2 words |
#14
|
||||
|
||||
The code already works with expressions containing multiple words. As indicated in post #7, though, you would need to modify it if you need something other than just the last word from the Find expression to be incorporated into the formfield.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
I did change the code as suggested in post #7 but when I use the string "my kitchen" it finds the words and then my screen keeps blinking and Word freezes.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to find text in between two characters and then format selected text? | qcom | Word | 5 | 02-19-2015 11:23 PM |
Form Fields - Create blank form text field with if statement? | LAssist2011 | Word | 5 | 12-14-2011 03:02 PM |
Macro to populate a text form field based on dropdown selection | koloa | Word | 0 | 10-20-2011 11:52 AM |
Need help on Macro 03- Find text - if text is blank then remove line | simpleonline1234 | Word VBA | 1 | 02-25-2011 02:28 AM |
Treating a text box like a form Text Field | jackaroo | Word | 0 | 08-18-2010 10:20 AM |