Prev: How to clear all built-in document properties?
Next: Determine if Selection is Text (Application Event Procedure)
From: LittleAnn on 10 Dec 2008 17:20 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 "Jean-Guy Marcil" wrote: > "LittleAnn" wrote: > > > 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?
From: Jean-Guy Marcil on 12 Dec 2008 08:42
"LittleAnn" wrote: > 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 |