Macro to delete a section keeping the rest of the document intact

S

Satya

Hi ,

My project has several word documents about the project.

We need to delete some sections/sub-sections before distributing the
documents.

I need a macro that will invoke a file, taken in section numbers to be
deleted and delete the sections keeping the table of contents and the rest of
the document intact.
Also to display a message inplace of the deleted section.

Thanks a lot for your help.
 
M

macropod

Hi Satya,

If you delete a Section, any Table of Contents links to headings in that Section will also be deleted and, next time the TOC is
updated, they'll disappear. You can get around that by converting the TOC to plain text before deleting the unwanted Section. Even
if there are no headings in the dleted Section, the page numbers will change. So, either the page numbers referred to by the TOC
will need updating or they won't match the page numbers in the document.

The same general principles apply to cross-references, footnotes, index entries etc. With cross-references, any pointing to the
deleted Section will also generate referencing errors.

Giev the above, you need to be clear as to what you require in each case.
 
S

Satya

Hi Macropod,

Thanks a lot for your help.

What if when the complete section gets deleted in all respects, it will
remove entries from TOC, will the TOC be updated for section numbers and page
numbers.
I will try to write the macro accordingly.
Any starting help on as how to do it would be appreciated.
Also what problems I may encounter in this scenario.

Thanks a lot
Satya

macropod said:
Hi Satya,

If you delete a Section, any Table of Contents links to headings in that Section will also be deleted and, next time the TOC is
updated, they'll disappear. You can get around that by converting the TOC to plain text before deleting the unwanted Section. Even
if there are no headings in the dleted Section, the page numbers will change. So, either the page numbers referred to by the TOC
will need updating or they won't match the page numbers in the document.

The same general principles apply to cross-references, footnotes, index entries etc. With cross-references, any pointing to the
deleted Section will also generate referencing errors.

Giev the above, you need to be clear as to what you require in each case.

--
Cheers
macropod
[MVP - Microsoft Word]


Satya said:
Hi ,

My project has several word documents about the project.

We need to delete some sections/sub-sections before distributing the
documents.

I need a macro that will invoke a file, taken in section numbers to be
deleted and delete the sections keeping the table of contents and the rest of
the document intact.
Also to display a message inplace of the deleted section.

Thanks a lot for your help.
 
M

macropod

Hi Satya,

In fairly simple terms:

Sub Demo()
Dim oSctn As Integer
oSctn = CInt(InputBox("Section # to Delete"))
With ActiveDocument
If .Sections.Count >= oSctn Then .Sections(oSctn).Range.Delete
.TablesOfContents(1).Update
End With
End Sub
Note that I've not attempted to do anything about updating cross-references etc. As a basic level, you could do this by adding
..Fields.Update before .TablesOfContents(1).Update.

--
Cheers
macropod
[MVP - Microsoft Word]


Satya said:
Hi Macropod,

Thanks a lot for your help.

What if when the complete section gets deleted in all respects, it will
remove entries from TOC, will the TOC be updated for section numbers and page
numbers.
I will try to write the macro accordingly.
Any starting help on as how to do it would be appreciated.
Also what problems I may encounter in this scenario.

Thanks a lot
Satya

macropod said:
Hi Satya,

If you delete a Section, any Table of Contents links to headings in that Section will also be deleted and, next time the TOC is
updated, they'll disappear. You can get around that by converting the TOC to plain text before deleting the unwanted Section.
Even
if there are no headings in the dleted Section, the page numbers will change. So, either the page numbers referred to by the TOC
will need updating or they won't match the page numbers in the document.

The same general principles apply to cross-references, footnotes, index entries etc. With cross-references, any pointing to the
deleted Section will also generate referencing errors.

Giev the above, you need to be clear as to what you require in each case.

--
Cheers
macropod
[MVP - Microsoft Word]


Satya said:
Hi ,

My project has several word documents about the project.

We need to delete some sections/sub-sections before distributing the
documents.

I need a macro that will invoke a file, taken in section numbers to be
deleted and delete the sections keeping the table of contents and the rest of
the document intact.
Also to display a message inplace of the deleted section.

Thanks a lot for your help.
 
S

Satya

Thanks a lot MacroPod.
I will try it out.
macropod said:
Hi Satya,

In fairly simple terms:

Sub Demo()
Dim oSctn As Integer
oSctn = CInt(InputBox("Section # to Delete"))
With ActiveDocument
If .Sections.Count >= oSctn Then .Sections(oSctn).Range.Delete
.TablesOfContents(1).Update
End With
End Sub
Note that I've not attempted to do anything about updating cross-references etc. As a basic level, you could do this by adding
..Fields.Update before .TablesOfContents(1).Update.

--
Cheers
macropod
[MVP - Microsoft Word]


Satya said:
Hi Macropod,

Thanks a lot for your help.

What if when the complete section gets deleted in all respects, it will
remove entries from TOC, will the TOC be updated for section numbers and page
numbers.
I will try to write the macro accordingly.
Any starting help on as how to do it would be appreciated.
Also what problems I may encounter in this scenario.

Thanks a lot
Satya

macropod said:
Hi Satya,

If you delete a Section, any Table of Contents links to headings in that Section will also be deleted and, next time the TOC is
updated, they'll disappear. You can get around that by converting the TOC to plain text before deleting the unwanted Section.
Even
if there are no headings in the dleted Section, the page numbers will change. So, either the page numbers referred to by the TOC
will need updating or they won't match the page numbers in the document.

The same general principles apply to cross-references, footnotes, index entries etc. With cross-references, any pointing to the
deleted Section will also generate referencing errors.

Giev the above, you need to be clear as to what you require in each case.

--
Cheers
macropod
[MVP - Microsoft Word]


Hi ,

My project has several word documents about the project.

We need to delete some sections/sub-sections before distributing the
documents.

I need a macro that will invoke a file, taken in section numbers to be
deleted and delete the sections keeping the table of contents and the rest of
the document intact.
Also to display a message inplace of the deleted section.

Thanks a lot for your help.
 

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