![]() |
#15
|
|||
|
|||
![]()
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 |
|
![]() |
||||
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 |
![]() |
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 |
![]() |
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 |