#1
|
|||
|
|||
Get paragraph number from multlilevel list style for use in TC field
Hi,
I am trying to insert a TC marker as part of a macro. It is inserting the TC marker correctly with the text I want but unfortunately doesn't seem to pick up the number which is applied to the current paragraph. The parapraph that I am applying my macro to is using a style which is in a multilevel list and has number '1' set. I followed Shauna's guide for setting up the numbering and it works really well. As an example it looks like this (with proper alignment!) Code:
1 Some text here 2 Some more text Code:
1 Some text here { TC "1 Some Text here" \f C \l "1" } 2 Some more text What I actually get is (note the omission of the '1') Code:
1 Some text here { TC "Some Text here" \f C \l "1" } 2 Some more text Some code with comments may help to explain a bit more: Code:
Dim myRange As Range myRange = Selection.Paragraphs(1).Range Dim thisPara as String Dim currentNumber as String Dim spaceChar as String Dim tcText as String spaceChar = " " ' This is the current paragraph's text thisPara = myRange ' I expected to be able to get the current number using the line below. But it's always blank. It doesn't even return "nothing" like the documentation says it should. I've tried calling it directly as well (e.g Selection.Paragraphs(1).Range.ListFormat.ListString) currentNumber = myRange.ListFormat.ListString ' Some concatenation... tcText = currentNumber & spaceChar & thisPara ActiveDocument.TableOfContents.MarkEntry Range:=myRange, Entry:=tcText, Level:=1 Does anyone know what I am doing wrong? Happy to provide more information if needed but I think there is enough information in my post. Thanks |
#2
|
||||
|
||||
A string is not the same thing as a Range and a paragraph range includes a return at the end of it
I'm not a fan of TC fields and you haven't explained why you aren't building the TOC using a style which avoids the need to place a TC field at all. The static TC field means that it will become out of step with the paragraph text or number if either were to change later. Code:
Sub StaticTC() Dim myRange As Range, thisPara As String, currentNumber As String Dim spaceChar As String, tcText As String spaceChar = " " Set myRange = Selection.Paragraphs(1).Range myRange.End = myRange.End - 1 thisPara = myRange.Text ' This is the current paragraph's text currentNumber = myRange.ListFormat.ListString 'will be empty if not an autonumber tcText = Trim(currentNumber & spaceChar & thisPara) 'remove leading and trailing spaces myRange.Collapse Direction:=wdCollapseEnd ActiveDocument.TablesOfContents.MarkEntry Range:=myRange, Entry:=tcText, Level:=1 End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank you, I will try that out tomorrow.
I am using TC fields because I need functionality to turn a level 1 style into a "heading". The only difference is that it becomes bold and slightly bigger, but you can't have two different styles at the same level in a multilevel list. Headings go in the TOC, other paragraphs using the same style do not. The rest of the TOC is built using styles. It's really quite a pain for me and I did post about it previously - unfortunately none of the proposed ideas really worked. Understood about the automatic updating of the TOC, that was something I'd considered but couldn't think of a solution. The macro I've got will also remove existing then add an updated TC field but that requires the user to interact with it. How would I add a nested ref field? That sounds ideal. |
#4
|
||||
|
||||
Ahh, but you CAN have two styles using the same multi-level list level.
Are you saying you want to have numbered headings and then have body text which uses the same related heading numbering. If you want to post a sample document showing what you are trying to get to, I can explain the steps to set up your styles correctly.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
I have attached a document that should help to explain
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Count number of periods and convert paragraph to a heading style | jeffreybrown | Word VBA | 7 | 10-19-2019 01:15 PM |
How do I create a field that will return the current paragraph number minus one in an automatic list | gugootz | Word | 1 | 11-23-2015 04:58 PM |
List Style Numbering picks up out of order number from LATER list | spthomas | Word | 12 | 12-16-2013 05:23 PM |
Trouble on Using List number style in Master Document | nattasiray | Word | 2 | 12-16-2011 08:01 PM |
Numbered List paragraph style prints out incorrectly when converted to PDF - Help! | kimrussell68 | Word | 0 | 02-02-2010 07:58 AM |