Macro for deleting multiple sections of a form.

C

CulinaryDisaster

I have a protected form which consists of 13 sections (20 pages in length).
Dependent upon the user's needs, he will only need to fill out 2 or 3
sections. My goal is to write a macro that will unprotect, delete the
unnecessary sections, and save it as a new file. There will be 12 macros
(one for each possible combination)which will reside on a special tool bar in
the document. I've done parts of all of this but have yet to link them
together in a single macro.

My experience in writing macros in VBA is close to non-existent. However, I
do copy, paste and edit with some proficiency. I have copied the
unprotect/protect macro from other posts. I found the following macro to
work when deleting a specific single section:

ActiveDocument.Sections(1).Range.Delete

How do I delete multiple sections - particularly if they are not sequencial
(i.e. Delete sections 2, 3, 5 through 10, 12 and 13)?
 
J

Jean-Guy Marcil

CulinaryDisaster said:
I have a protected form which consists of 13 sections (20 pages in length).
Dependent upon the user's needs, he will only need to fill out 2 or 3
sections. My goal is to write a macro that will unprotect, delete the
unnecessary sections, and save it as a new file. There will be 12 macros
(one for each possible combination)which will reside on a special tool bar in
the document. I've done parts of all of this but have yet to link them
together in a single macro.

My experience in writing macros in VBA is close to non-existent. However, I
do copy, paste and edit with some proficiency. I have copied the
unprotect/protect macro from other posts. I found the following macro to
work when deleting a specific single section:

ActiveDocument.Sections(1).Range.Delete

How do I delete multiple sections - particularly if they are not sequencial
(i.e. Delete sections 2, 3, 5 through 10, 12 and 13)?

ActiveDocument.Sections(1).Range.Delete
deletes the first section in the document.

If you have a contiguous range of sections to delete, you can try something
like this:


Dim rgeDelete As Range

With ActiveDocument
Set rgeDelete = .Range(.Sections(2).Range.Start, .Sections(5).Range.End)
End With
rgeDelete.Delete

This will delete sections 2 to 5.

However, if you delete sections one by one, beware of the reindexing (or
renumbering of the sections).

If you want to delete sections 2 and 4 in a 5-section document, you need to
use:

ActiveDocument.Sections(2).Range.Delete
ActiveDocument.Sections(3).Range.Delete

Because once you delete section 2, what use to be section 4 is now section 3.

One way around that is to delete from the end:

ActiveDocument.Sections(4).Range.Delete
ActiveDocument.Sections(2).Range.Delete

When you delete section 4, section 2 still is section 2...
 
C

CulinaryDisaster

Thank you so much for your help. This worked perfectly. I have amazed and
dazzled my colleagues. They did not think I could pull this off. It would
not have been possible without the information provided in this forum. This
is a real treasure trove.
 

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