Repeating Macro

S

sg

I have a form that uses checkboxes for the user to determine which sections
should stay and which sections should go. When they are finished filling in
the form, a button runs the following macro:

Sub DeleteSection()
'
Dim oFF As FormField
On Error Resume Next
ActiveDocument.Unprotect
On Error GoTo 0
Set oFF = Selection.Sections(1).Range.FormFields(1)
If oFF.Checkbox.Value = False Then
Selection.Sections(1).Range.Delete
Else
Selection.Sections(1).Range.FormFields(1).Delete
End If
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub

It works great except that it only works for one section at a time instead
of repeating through the whole document. I have read other posts that talk
about looping but it seems everyone else is looking to use their macro for
formatting instead of deleting. Can I loop my macro?

Thanks in advance for any help.
 
G

Graham Mayor

Something along the lines of

Sub DeleteSection()
Dim oFF As FormField
On Error Resume Next
ActiveDocument.Unprotect
On Error GoTo 0
For i = ActiveDocument.Sections.Count To 1 Step -1
Set oFF = ActiveDocument.Sections(i).Range.FormFields(1)
If oFF.CheckBox.Value = False Then
ActiveDocument.Sections(i).Range.Delete
Else
ActiveDocument.Sections(i).Range.FormFields(1).Delete
End If
Next i
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

sg

Thank you for your quick reply. I tried what you suggested, but when I run
the macro, I get

Run Time Error 5941. The requested member of the collection does not exist.

When I click on Debug, the following line is highlighted:

Set oFF = ActiveDocument.Sections(i).Range.FormFields(1)

Unfortunately, I am not experienced enough at VBA to know what this means!
Please help me!
 
S

sg

I should also note that when I click on Ok and stop the debugger, everything
appears to have worked. I don't know if that makes a difference.
 
G

Graham Mayor

It appears that not all the sections in your document have a formfield. The
following should work whether or not the sections have a form field and will
only take note of the first form field in the section and then only if it is
a checkbox form field.

Sub DeleteSection()
Dim oFF As FormField
ActiveDocument.Unprotect
'start at the end of the file and work back to the beginning
For i = ActiveDocument.Sections.Count To 1 Step -1
'If the section contains a form field the count will not be 0
If ActiveDocument.Sections(i).Range.FormFields.Count <> 0 Then
'But if there is, set the first form field as a variable
Set oFF = ActiveDocument.Sections(i).Range.FormFields(1)
'If that first form field is a check box
If oFF.Type = wdFieldFormCheckBox Then
'and its value is unchecked
If oFF.CheckBox.Value = False Then
'delete the section containing it
ActiveDocument.Sections(i).Range.Delete
Else
'otherwise delete the check box
ActiveDocument.Sections(i).Range.FormFields(1).Delete
End If
End If
End If
Next i
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

sg

That is so awesome - worked like a charm!!!

Thank you so much for your help. I really need to learn VB.

Its so awesome to get help from you guys on this forum. I have received so
much help - thank you for the time you give to help others.
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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