Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-10-2012, 04:03 PM
iiiiifffff iiiiifffff is offline Macro to find text and replace with form field containing that text Windows XP Macro to find text and replace with form field containing that text Office 2010 32bit
Novice
Macro to find text and replace with form field containing that text
 
Join Date: Apr 2012
Posts: 5
iiiiifffff is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 04-10-2012, 06:21 PM
macropod's Avatar
macropod macropod is offline Macro to find text and replace with form field containing that text Windows 7 64bit Macro to find text and replace with form field containing that text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 04-10-2012, 07:47 PM
iiiiifffff iiiiifffff is offline Macro to find text and replace with form field containing that text Windows XP Macro to find text and replace with form field containing that text Office 2010 32bit
Novice
Macro to find text and replace with form field containing that text
 
Join Date: Apr 2012
Posts: 5
iiiiifffff is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 04-10-2012, 08:54 PM
macropod's Avatar
macropod macropod is offline Macro to find text and replace with form field containing that text Windows 7 64bit Macro to find text and replace with form field containing that text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 04-10-2012, 09:15 PM
iiiiifffff iiiiifffff is offline Macro to find text and replace with form field containing that text Windows XP Macro to find text and replace with form field containing that text Office 2010 32bit
Novice
Macro to find text and replace with form field containing that text
 
Join Date: Apr 2012
Posts: 5
iiiiifffff is on a distinguished road
Default

Wow! Thanks! That did it. I really appreciate your help.

Ian
Reply With Quote
  #6  
Old 05-04-2012, 11:35 AM
HFrostick HFrostick is offline Macro to find text and replace with form field containing that text Windows XP Macro to find text and replace with form field containing that text Office 2010 64bit
Novice
 
Join Date: May 2012
Posts: 2
HFrostick is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 05-04-2012, 04:08 PM
macropod's Avatar
macropod macropod is offline Macro to find text and replace with form field containing that text Windows 7 64bit Macro to find text and replace with form field containing that text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #8  
Old 05-06-2012, 11:48 PM
HFrostick HFrostick is offline Macro to find text and replace with form field containing that text Windows XP Macro to find text and replace with form field containing that text Office 2010 64bit
Novice
 
Join Date: May 2012
Posts: 2
HFrostick is on a distinguished road
Default

Works perfectly. Thanks very much.

Harold
Reply With Quote
  #9  
Old 02-15-2015, 06:55 PM
mmmaaa mmmaaa is offline Macro to find text and replace with form field containing that text Windows 8 Macro to find text and replace with form field containing that text Office 2010 64bit
Novice
 
Join Date: Feb 2015
Posts: 2
mmmaaa is on a distinguished road
Default 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
Reply With Quote
  #10  
Old 02-15-2015, 09:56 PM
macropod's Avatar
macropod macropod is offline Macro to find text and replace with form field containing that text Windows 7 64bit Macro to find text and replace with form field containing that text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #11  
Old 02-16-2015, 01:38 PM
mmmaaa mmmaaa is offline Macro to find text and replace with form field containing that text Windows 8 Macro to find text and replace with form field containing that text Office 2010 64bit
Novice
 
Join Date: Feb 2015
Posts: 2
mmmaaa is on a distinguished road
Default

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.
Reply With Quote
  #12  
Old 02-16-2015, 03:19 PM
macropod's Avatar
macropod macropod is offline Macro to find text and replace with form field containing that text Windows 7 64bit Macro to find text and replace with form field containing that text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #13  
Old 06-03-2016, 12:45 PM
edsulst edsulst is offline Macro to find text and replace with form field containing that text Windows 10 Macro to find text and replace with form field containing that text Office 2010 32bit
Novice
 
Join Date: Jun 2016
Posts: 3
edsulst is on a distinguished road
Default

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
Reply With Quote
  #14  
Old 06-03-2016, 03:09 PM
macropod's Avatar
macropod macropod is offline Macro to find text and replace with form field containing that text Windows 7 64bit Macro to find text and replace with form field containing that text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #15  
Old 06-03-2016, 11:12 PM
edsulst edsulst is offline Macro to find text and replace with form field containing that text Windows 10 Macro to find text and replace with form field containing that text Office 2010 32bit
Novice
 
Join Date: Jun 2016
Posts: 3
edsulst is on a distinguished road
Default

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.
Reply With Quote
Reply

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
Macro to find text and replace with form field containing that text 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
Macro to find text and replace with form field containing that text 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:56 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft