PDA

View Full Version : Input-Dependent Chapter Tabs?



gotmatt
07-21-2011, 08:58 AM
Hi all,

I'm following up from a previous post about setting up AutoTexts in headers and footers (http://www.vbaexpress.com/forum/showthread.php?t=38255) (http://www.vbaexpress.com/forum/showthread.php?t=38255%29).

I succeeded in my goal of applying different AutoTexts to the first/odd/even headers of the document, but now I've run into something a bit more complex...

My goal is to apply unique AutoText entries to each chapter of the document. These AutoText entries place different colored tabs at the top of headers, later to be specified as only odd headers. So, Chapter 1 would have a blue tab, Chapter 2 would have a green tab, and chapter 3 (etc.) would have another color... I know for certain that each chapter is contained in a document section.

Unfortunately, from document to document, Chapter 1 may begin in any section, and the last chapter may be found in any following section. So far, I have created a form (w/ textboxes) that retrieves this important information from the editor. They enter which section the first and last chapters are located, and the VBA defines them as Long variables "StartSection" and "EndSection."

StartSection = UserFrm.StartSectionTextBox.Value
EndSection = UserFrm.EndSectionTextBox.Value
From here, I need to have the VBA code, starting in Section(StartSection), apply the autotext for the blue tab. Once it has applied that blue tab AutoText to every odd header in Section(StartSection), it should move to the next section (StartSection+1) and apply the green tab Autotext to all odd headers.

Chapter 1 will always have the same colored tab, so I needn't worry about that changing. The number of chapters, however, will change. I may need this loop to end at section 10, or maybe 5. Given that I know the start section and end section, does anyone have a suggestion on how to best cycle through ONLY these sections?

A forum member "Frosty" was kind enough to supply me with this cycling method...
Dim oDoc as Document
Dim hf as HeaderFooter
Dim x As Integer
Dim y As Integer

For x = 1 To oDoc.Sections.Count
With oDoc.Sections(x)
For y = 1 To .Headers.Count
Set hf = .Headers(y)
Set hf = .Footers(y)
Next
End With
Next

I tried to tailor this to my needs with...
Dim TabHeader as HeaderFooter
Dim c as Integer ' (or should it be Long?)
Dim D as Integer ' (or Long?)
For c = oDoc.Sections(StartSection) To oDoc.Sections(EndSection)
With oDoc.Sections(c)
For d = 1 To oDoc.Sections(EndSection).Headers.Count
Set TabHeader = .Headers(d)
Next
End With
Next
I'm under the impression, however, that if StartSection=3 and EndSection=5, then c=2... which means "With oDoc.Section(c)" just has the code start at Section(2)?

Sorry this explanation is sooooo long, but I just want to clarify everything. So far my "For c = oDoc.Sections(StartSection) To" is getting a "type mismatch" error from the debugger, so I'm looking into that now. Thanks for any advice or help you can offer! I hope this helps someone else approach a similar problem in the process! :)

Thanks for your time and consideration!

gotmatt
07-21-2011, 10:05 AM
*Some added clarification:

Say my first chapter started in Section 4 and my last chapter ends in Section 9...

Per the UserFrm (Text Boxes), variables "StartSection" and "EndSection" would be defined as 4 and 9, respectively.

Section 4 would require AutoText "HeaderTabBlue" on all its odd headers.
Section 5 would require AutoText "HeaderTabGreen" on all its odd headers.
Section 6 would require AutoText "HeaderTab(etc.)" on all its odd headers.
Section 7....
Section 8....
Section 9 would require AutoText "HeaderTabRed" on all its odd headers.
*At this point, the procedure would end.

I plan on using the same counting method for sections 2 through "StartSection" and for sections "EndSection(+1) through ActiveDocument.Sections.Count(-1). That should maintain the information in the first and last section, hopefully. But once I understand the above theory, making these final changes will be a breeze!

Thanks again!

gotmatt
07-22-2011, 02:16 PM
***I've figured out the solution***

I'll update this post with my findings later tonight.