TOC - creating Macro/Userform to update depending oon Heading Leve

L

LittleAnn

Hi

I am creating a report and would like to use a Userform so that when the
report is complete that the user can press a command button to automatically
generate the Table of Contents.

The report itself would have headings from levels 1 to 4 and sometimes may
or maynot have Appendices. I would like the userform to give the options to
Uncheck the levels if they are not being used in that particular document i.e
number of levels or if appendices are not needed and then for the TOC to
update according to what the User ticks or unticks on the userform.

Is this possible and if so what is the best way around generating a TOC from
a command button????
 
J

Jean-Guy Marcil

LittleAnn said:
Hi

I am creating a report and would like to use a Userform so that when the
report is complete that the user can press a command button to automatically
generate the Table of Contents.

The report itself would have headings from levels 1 to 4 and sometimes may
or maynot have Appendices. I would like the userform to give the options to
Uncheck the levels if they are not being used in that particular document i.e
number of levels or if appendices are not needed and then for the TOC to
update according to what the User ticks or unticks on the userform.

Is this possible and if so what is the best way around generating a TOC from
a command button????

Yes, but this is not a few lines of code one can post in a few minutes.

How familiar are you with generating userform?
How about VBA?
Do you understand the intricacies of the TOC field (Which you need to know
in order to generate a TOC through VBA)?
What Word version?
 
L

LittleAnn

I only have beginner knowledge of VBA, I have set up a userform for my report
for when it is opened to insert main text into the document i.e Report Title,
Client name, date of issue etc, I have also set up various macros to insert
Appendix pages and change orientation of pages from portrait to landscape and
vice versa. I understand the components of the TOC field as have advanced
word skills but not in the programming side of things. I am using Word 2003.


Any help would be great, even if you know of a site I may be able to look up
that will show me how to get started would be appreciated.

Thanks
 
J

Jean-Guy Marcil

LittleAnn said:
I only have beginner knowledge of VBA, I have set up a userform for my report
for when it is opened to insert main text into the document i.e Report Title,
Client name, date of issue etc, I have also set up various macros to insert
Appendix pages and change orientation of pages from portrait to landscape and
vice versa. I understand the components of the TOC field as have advanced
word skills but not in the programming side of things. I am using Word 2003.


Any help would be great, even if you know of a site I may be able to look up
that will show me how to get started would be appreciated.

Create a section for the ToC.
This way, when coding, all you need to do is refer to that section range to
know where to insert/update the ToC.
Make sure you are familliar with the TOC field, go there, in case you need a
refresher:
http://word.mvps.org/FAQs/Formatting/TOCSwitches.htm
On your user form, use a ComboBox to let the user select up to how many
levels they want in their TOC.
Finally, with your code, insert the TOC field in the aforementioned section:
For instance:

Dim rgeTOC As Range

Set rgeTOC = ActiveDocument.Sections(2).Range

rgeTOC.Collapse wdCollapseStart

rgeTOC.Fields.Add rgeTOC, wdFieldEmpty, "TOC \o ""1-3""", False

You will need to build the string differently in order to account for the
user selection, maybe something like:

rgeTOC.Fields.Add rgeTOC, wdFieldEmpty, _
"TOC \o ""1-" & Me.ComboBox1.Value & """", False
 

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