Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-16-2023, 06:08 AM
TallTrees TallTrees is offline Get paragraph number from multlilevel list style for use in TC field Windows 10 Get paragraph number from multlilevel list style for use in TC field Office 2021
Novice
Get paragraph number from multlilevel list style for use in TC field
 
Join Date: Jul 2022
Posts: 8
TallTrees is on a distinguished road
Default 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
Desired output is something like this if I was to run the macro at the first paragraph:

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
It seems that the Range.ListFormat.ListString is empty. Before trying to use this I was attempting to just select the current paragraph, hoping it'd also grab the number, but it just finds the text of the paragraph.


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
Reply With Quote
  #2  
Old 03-16-2023, 05:02 PM
Guessed's Avatar
Guessed Guessed is offline Get paragraph number from multlilevel list style for use in TC field Windows 10 Get paragraph number from multlilevel list style for use in TC field Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
If you absolutely can't build the TOC with styles, perhaps a better approach would be to build the TC field with nested Ref fields so that the TOC entries dynamically update with the current paragraph number and text.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 03-16-2023, 05:28 PM
TallTrees TallTrees is offline Get paragraph number from multlilevel list style for use in TC field Windows 10 Get paragraph number from multlilevel list style for use in TC field Office 2021
Novice
Get paragraph number from multlilevel list style for use in TC field
 
Join Date: Jul 2022
Posts: 8
TallTrees is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 03-16-2023, 06:28 PM
Guessed's Avatar
Guessed Guessed is offline Get paragraph number from multlilevel list style for use in TC field Windows 10 Get paragraph number from multlilevel list style for use in TC field Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 03-17-2023, 02:11 AM
TallTrees TallTrees is offline Get paragraph number from multlilevel list style for use in TC field Windows 10 Get paragraph number from multlilevel list style for use in TC field Office 2021
Novice
Get paragraph number from multlilevel list style for use in TC field
 
Join Date: Jul 2022
Posts: 8
TallTrees is on a distinguished road
Default

I have attached a document that should help to explain
Attached Files
File Type: docx Example - Styles - Copy.docx (13.8 KB, 4 views)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get paragraph number from multlilevel list style for use in TC field Count number of periods and convert paragraph to a heading style jeffreybrown Word VBA 7 10-19-2019 01:15 PM
Get paragraph number from multlilevel list style for use in TC field 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
Get paragraph number from multlilevel list style for use in TC field List Style Numbering picks up out of order number from LATER list spthomas Word 12 12-16-2013 05:23 PM
Get paragraph number from multlilevel list style for use in TC field 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:32 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft