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 / Document Management / March 2008

Tip: Looking for answers? Try searching our database.

Macro for deleting multiple sections of a form.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CulinaryDisaster - 26 Mar 2008 16:33 GMT
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)?
Jean-Guy Marcil - 26 Mar 2008 17:11 GMT
> 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
[quoted text clipped - 13 lines]
> 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...
CulinaryDisaster - 27 Mar 2008 16:10 GMT
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.

> > 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
[quoted text clipped - 46 lines]
>
> When you delete section 4, section 2 still is section 2...
 
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.