![]() |
#1
|
|||
|
|||
![]()
I need to develop a macro for adding a continuation heading based on a template using 7 outline levels. The outline numbering is as follows:
Heading 1: 1.0 Heading 2 and Heading 2 TOC: 1.1 Heading 3 and Heading 3 TOC: 1.1.1 Heading 4: A. Heading 5: 1. Heading 6: a. Heading 7: 1) We require a continuation heading for any page that starts with a Heading 4 or higher. Some examples would be 6.1.2.B. (continued), 7.2.1.A.13. (continued), 5.3.2.B.2.b. (continued), etc. In a perfect world this would be automatic, but manual would be better than the time consuming method of manually cross-referencing the preceeding parent step and manually typing (continued). Last edited by Charles Kenyon; 03-11-2015 at 09:31 AM. Reason: Mark as solved |
#2
|
||||
|
||||
![]()
Pagination is dynamic - that is, it depends on the page setup, printer chosen and content already in the document before it can be determined. This makes your request difficult to answer simply.
In addition, there would be numerous special cases that would need to be addressed. Eg. What happens when the page starts with a Heading 1, 2 or 3. What happens when a table continues across a pagebreak? If you broke the table to insert this continued paragraph then the table headings won't repeat. I envision that a macro could be created to roll through the document and create the required cross-reference paragraphs. I would recommend a crossref field since you can add a switch which assembles the full heading number context (ie returns the 6.1.2.B even though the heading itself only says B). The cross ref field requires that you identify the nearest preceding heading and bookmark it. You would probably need to include the ability to strip out these entries since you would need to rerun the macro every time the document was edited or opened on another machine.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
![]()
I know this can be done because I worked with a template once that could do this during the pagination process. I can settle for manual. I need a macro that will look for the Heading style just downstream of the cursor location and then cross-reference the parent Heading style just upstream.
|
#4
|
||||
|
||||
![]()
I can help you with a manual method but I need to understand the following: What does the downstream heading style have to do with it? I thought you only wanted the cross-ref to point back to the preceding heading.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
![]()
I would think you would have to know what your downstream style is so you would know what parent to look for upstream. Say the first thing on a page is a Note box and the step after the Note (downstream) is B. (Heading 4). I would need to look upstream for the last Heading 3 (say 6.1.4) and make a cross-reference to it. Another example would be where the first step is 3) (Heading 7) and I would need to cross-reference to the preceding Heading 6 so my continuation header would look something like 6.1.4.B.2.a. If you look at the Headings I listed in the question, you notice the is no period at the end of the Heading 3 numbering. When we manually cross-reference we get 6.1.4B.2.a. with no period added between the 4 and B. I can't find a solution for that either.
|
#6
|
||||
|
||||
![]()
I still don't see how the following heading is relevant. If I see a Heading 4 on this page then it might still be preceded by any heading level (although a Heading 1 or 2 would indicate poor document structure).
Have a look at the following attempt at understanding your requirement. This would need to be modified further to name the bookmark created uniquely but I believe it contains all the elements to get you going. You are not going to get a solution with the missing period if you retain the cross-ref but you could choose to unlink it and then add the period into the right position. I would also recommend you create a specific style for this continuation paragraph which includes a 'page break before' setting otherwise it will often end up on the preceding page. Code:
Sub ProofOfConcept() 'macro by Andrew Lockton Dim aRng As Range Set aRng = ActiveDocument.Bookmarks("\page").Range aRng.InsertParagraphBefore With aRng.Paragraphs(1) .Style = "Normal" .OutlinePromote Debug.Print .OutlineLevel If .OutlineLevel > 3 Then .Style = "Normal" aRng.Collapse Direction:=wdCollapseStart aRng.Select Selection.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1 ActiveDocument.Bookmarks.Add Name:="ABC", Range:=Selection.Range aRng.InsertAfter " continued" aRng.Collapse Direction:=wdCollapseStart ActiveDocument.Fields.Add Range:=aRng, Text:="Ref ABC \w \h" aRng.Select Else aRng.Paragraphs(1).Range.Delete End If End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
![]()
When I run this code it picks the preceding heading style and not the parent. Example: the step at the top of the page is 4. and the parent step is 6.2.1.A. Your code returns 6.2.1.a.3, not 6.2.1.A. I also appears to be a onetime use due to the bookmark. I may have a large procedure that requires many different continuation headers.
|
#8
|
||||
|
||||
![]()
Yes, I told you it was a one-time use bookmark and that it needed additional code to work in multiple cases. The purpose of the macro I posted was to show the concept of how you might perform the task.
I assume that when the first paragraph on the page is not a heading then you want the code as provided. If it is a heading then you want to return the level above that. Is that right?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
![]()
All the steps are Headings. If I have a Heading 5 at the top of the page, I want to return the previous Heading 4.
|
#10
|
|||
|
|||
![]()
Problem not solved. Is there anyone out there with a possible solution?
|
#11
|
||||
|
||||
![]()
See if http://www.gmayor.com/catchwords.htm helps.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#12
|
||||
|
||||
![]()
Try the next iteration of the concept. It still has the bookmark uniqueness issue that I spoke about but the rest of it should show you how the code can arrive at the preceding heading's parent.
Code:
Sub ProofOfConcept() 'macro by Andrew Lockton Dim aRng As Range, iLev As Integer Set aRng = ActiveDocument.Bookmarks("\page").Range aRng.InsertParagraphBefore With aRng.Paragraphs(1) .Style = "Normal" .OutlinePromote iLev = .OutlineLevel If iLev > 3 Then .Style = "Normal" aRng.Collapse Direction:=wdCollapseStart aRng.Select Selection.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1 While Selection.Paragraphs(1).OutlineLevel <> iLev - 1 Selection.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1 Wend ActiveDocument.Bookmarks.Add Name:="ABC", Range:=Selection.Range aRng.InsertAfter " continued" aRng.Collapse Direction:=wdCollapseStart ActiveDocument.Fields.Add Range:=aRng, Text:="Ref ABC \w \h" aRng.Select Else aRng.Paragraphs(1).Range.Delete End If End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#13
|
|||
|
|||
![]()
In reply to gmayor, I don't see how your suggestion could be applied in my situation. I'm not trying to put anything in the header or footer but at the top of the page. I do see some good information about waiting until the document is finished before running the macro but it doesn't really show me how to note the first appearance of a heading on a page (it could come after a table or graphic) and then find it's parent header on the preceding page (could be several pages back).
|
#14
|
|||
|
|||
![]()
The last iteration worked! Thanks for the solution.
|
#15
|
||||
|
||||
![]()
You should be able to find code online to construct a valid and unique bookmark name. An example of such a macro can be found here http://www.vbaexpress.com/forum/show...-VBA-Bookmarks
Note that Greg's code is not bulletproof but it is a good case study to get you over the next hurdle. Shortcomings of that code, which may or may not matter to you are: - Doesn't verify whether the bookmark name already exists - Doesn't truncate string to the maximum length allowed by bookmarks
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
jbvalen | Word VBA | 5 | 05-04-2017 05:03 PM |
Word Mixing Numbered Headings with Numbered List | Tess0 | Word | 11 | 07-15-2014 05:25 AM |
![]() |
triodia | Word | 2 | 05-07-2012 02:03 AM |
![]() |
Caroline | Word | 5 | 03-14-2011 09:09 AM |
XML marking of Auto-numbered Headings | crose | Word | 0 | 12-17-2009 09:55 PM |