Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retreive list numbering, Word 2

Status
Not open for further replies.

sylve

Programmer
Jan 18, 2005
58
CA
Hello.
I have a document with a multilevel, outline-numbered list. A section break seperates the first level items. Looks a little like this :

1. Chapter 1
1.1 subtitle

------section break----------

2. Chapter 2
2.1 subtitle
2.2 subtitle


Now for each section, Ì need to print a footer with the Number of the chapter. When i select the text in the range though, it seems to exclude the number. Now i'm hitting a wall.

Does anybody know how to get the paragraph number into a string?

For Each sec In ActiveDocument.Sections
For Each para In sec.Range.Paragraphs 'goes through paragraphs in the current section
If para.OutlineLevel = 1 Then 'level 1 title
h1 = para.Range.Text 'TO CHANGE
end if
Next para

sec.Footers(wdHeaderFooterPrimary).Range = h1

next sec
 
You are asking for a few things, some of which contradict each other.

The Chaper number? Grab the string at the END of the text.

2. Chapter 2 - grab THIS 2, not the first one.

The first 2 is NOT text really. Well it si text, but it is calculated, and is always in the present. Do you notice you can not select it? You also can not pick it up as a Range object.

Paragraph number? I presume you actually mean outline number.

What exactly are you trying to do?

Gerry
 
Actually no, the "Chapter 2" is not the actual text. A title for the chapter is there. What i meant by paragraph number is : a paragraph is (or not) a member of a list. What i actually needed is the indice, index or whatever number of that list item.

In any case... i found my answer thanks. I just incremented a interger. Here is my macro:

Sub CreateFooter()
Dim h1 As String
Dim h2 As String
Dim i As Integer
Dim y As Integer
Dim myRange As Range

i = ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(1).StartAt

For Each sec In ActiveDocument.Sections
y = ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(2).StartAt
For Each para In sec.Range.Paragraphs 'goes through paragraphs in the current section
If para.OutlineLevel = 1 Then 'level 1 title
h1 = i
i = i + 1
ElseIf para.OutlineLevel = 2 Then
h2 = y
y = y + 1
End If
Next para
sec.Footers(wdHeaderFooterPrimary).Range.Text = h1 & "-" & h2
sec.Footers(wdHeaderFooterPrimary).PageNumbers.Add

Next sec
End Sub
 
To my simple eyes this looks as if you could have achieved the same thing without code.

For example, the following field in a header:

{STYLEREF "Heading 1"}

will cause the header to show the content of the last HEADING 1 style which would be the chapter name/number. As as long as your chapter headings are in a consistent style the field will work.

There are optional parameters so if you are in the habit of starting chapters halfway down the page you can control whether it is the new or previous chapter that is shown.
 
Did i mention each chapter was a different sub document?

I like your idea though... Is there any way you can put in that {STYLEREF "Heading 1"} field into code?

The instance is, not every header/footer is the same... They have to be dynamic because of the master/subdocument strategy
 
I am no expert on master/sub documents. In earlier versions of Word the process was flaky and there were frequent reports of lost work.

I just created a simple case with 3 chapters and 3 sub-documents. In the master document header I put the styleref field and it worked as expected picking up the chapter names out of the sub-documents.

The only oddity was when I opened a sub-document on its own there was no header. I tried putting a header in the middle sub document. When I print previewed the master that header actually appeared in the second page of chapter 2. Presumably this is because of the positioning of section breaks.

So my feeling is that styleref works ok if put in the master document and if you avoid headers in individual sub documents. However, I am no expert on master/sub ducuments as I indicated previously.
 
Picking up your point about code, I used the macro recorder to record the action of inserting a styleref field.

The answeer was:
Code:
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "STYLEREF ""Heading 1""", PreserveFormatting:=True
 
Wow, never thought of using the recorder... Thanks for the idea.

As for the sub document, when you create a header in a master document, it remains there. The subdocuments are not usually changed so you can use 1 subdoc. in 2 masters (which is why i am doing macros that can be applied to any master).

Thank you all for your help on this, i appreciate the replies.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top