Delete line containing a form field

M

Melinda Chase

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
 
J

Jay Freedman

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

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
M

Melinda Chase

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top