Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2007

Tip: Looking for answers? Try searching our database.

Delete line containing a form field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Melinda Chase - 14 Sep 2007 15:24 GMT
Hello,
I have a template that is used to create recipes for upload to a website.
The person typing in the recipes is not very computer literate, so I made a
form with fields for him to fill in with ingredients.  The fields' default
text is "Ingredient".  I put in 15 lines for ingredients.  Obviously, not
all recipes have that many ingredients.  I'd like to write a macro to delete
the lines that don't have ingredients in them, i.e., they say "Ingredient"
in the form field.  To make it more complicated, I'd like to delete the
entire line, not just the form field.  Is there a way to do this?  I've
tried a find and replace, but that doesn't seem to work with form fields.
Anyone have any guidance?
Thanks!
Melinda
Jay Freedman - 14 Sep 2007 16:09 GMT
This macro will delete the entire paragraph (note: Word doesn't know much
about "lines", it's geared toward paragraphs, but in this case I suspect
they're the same thing) if the form field contains its default text
(whatever that is, whether "Ingredient" or any other text, or five
nonbreaking spaces if the default isn't set).

Sub CleanUp()
   Dim ffld As FormField

   If ActiveDocument.ProtectionType <> wdNoProtection Then
       ActiveDocument.Unprotect
   End If

   For Each ffld In ActiveDocument.FormFields
       With ffld
           If .Type = wdFieldFormTextInput Then
               If .Result = .TextInput.Default Then
                   .Range.Paragraphs(1).Range.Delete
               End If
           End If
       End With
   Next

   ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
       noreset:=True
End Sub

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Hello,
> I have a template that is used to create recipes for upload to a
[quoted text clipped - 9 lines]
> form fields. Anyone have any guidance? Thanks!
> Melinda
Melinda Chase - 14 Sep 2007 19:59 GMT
Thanks Jay!
That was exactly what I needed.
You are correct, I had a paragraph return at the end of each line, so
deleting the paragraph is fine.
Thanks again.
Melinda

> This macro will delete the entire paragraph (note: Word doesn't know much
> about "lines", it's geared toward paragraphs, but in this case I suspect
[quoted text clipped - 36 lines]
>> form fields. Anyone have any guidance? Thanks!
>> Melinda
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.