Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-16-2023, 03:41 PM
TallTrees TallTrees is offline How to insert a cross reference nested in a TC field and automatically get the correct autonumber? Windows 10 How to insert a cross reference nested in a TC field and automatically get the correct autonumber? Office 2021
Novice
How to insert a cross reference nested in a TC field and automatically get the correct autonumber?
 
Join Date: Jul 2022
Posts: 8
TallTrees is on a distinguished road
Default How to insert a cross reference nested in a TC field and automatically get the correct autonumber?

Hi,




I'm trying to insert a TC field and include a cross reference to the current paragraph inside of it (nested). I can easily do this if adding it manually by inserting the fields but am struggling to do it via VBA. This is the rough code I have so far:


Code:
Dim currentParagraph as Paragraph
Set currentParagraph = Selection.Paragraphs(1)
currentParagraph.Range.End = currentParagraph.Range.End - 1

Dim level as Long
level = 1
Dim crossRefText as String
crossRefText = "{REF }" & vbTab & Trim(currentParagraph.Range.Text) & " \l " & level

Dim currentNumber as String
currentNumber = currentParagraph.Range.ListFormat.ListString

Dim oRng as Range
Set oRng = currentParagraph.Range
With oRng
    .Fields.Add Range:=oRng, Type:=wdFieldTOCEntry, Text:=crossRefText, PreserveFormatting:=False
    .Collapse 1
    .MoveEndUntil "}"
    .End = .End + 1
    .MoveStartUntil "{"
    .Text = ""
    .InsertCrossReference wdRefTypeNumberedItem, wdNumberFullContext, currentNumber, True, False, True, "."
    .Fields.Update
End With
I've been partially successful with this but have ran into some issues & have some questions. I'll try to be brief:


  1. I thought that STYLEREF fields might be the answer but can't have one in the same paragraph as the one I am trying to reference. Plus that seems a bit hit and miss as to which paragraph it gets the text or number from. So I have resorted to trying to add a cross reference instead.
  2. Code to nest fields is difficult to get right, I feel like it's not supported! Or I am doing something entirely wrong.
  3. How do I get the bookmark for a cross reference before I have inserted one? Or do I need to insert my own? If I have to insert a bookmark for each paragraph (via VBA), I feel like this will quickly become unmanageable with several multi level lists with similar numbering, even if I prefix the names. If I pass in the paragraph number as I am now, and there are two paragraphs numbered "1", how does the "InsertCrossReference" function know which one to reference? This is why I'd like it to just apply to the current paragraph as I can isolate those which need a TC field and loop through them.
  4. The question above could potentially be answered by answering - how do I insert a cross reference to the current paragraph?
  5. I need this to apply to many paragraphs "automatically" (I'll create a reusuable method which I can pass things into), whereby it picks up the paragraph autonumber & text and puts that into the field.

    With some other code I don't have to hand right now, I have this working for just the TC fields already but need the cross reference in place of the plaintext number that's there currently. Otherwise small changes in the document can result in an out of date TOC.
  6. Is there an easy way to just go to the end of the current paragraph and pop the field there? It seems a bit hit and miss as to where it gets inserted.

I have attached a couple of screenshots showing what i'd like to see, with and without field codes on. The document is attached, too and is a good representation of a small example of one of the multilevel lists being used.



You may ask: Why am I using TC fields rather than styles? I am using styles for some of the TOC, but I resorted to TC fields because I couldn't get Word to behave itself with the requirements of our business. It is working, but would work significantly better with cross references. There were a number of issues with restarting numbering, incorrect formatting, etc. I've posted about this before but I don't think it's too relevant at the moment.


Hopefully this makes sense! Thanks
Attached Images
File Type: jpg Fields 1.jpg (97.6 KB, 13 views)
File Type: jpg Fields 2.jpg (81.5 KB, 12 views)
Attached Files
File Type: docx Example document for cross refs.docx (17.4 KB, 1 views)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert a cross reference nested in a TC field and automatically get the correct autonumber? How do I create a cross reference to a changeable Sequence field that can automatically update KRoger Word 1 08-27-2018 03:08 PM
How to insert a cross reference nested in a TC field and automatically get the correct autonumber? Insert a cross-reference in a caption RomFigo Word 9 09-25-2015 12:32 AM
cross-reference to field eNGiNe Word 1 02-27-2015 07:46 PM
How to insert a cross reference nested in a TC field and automatically get the correct autonumber? Help with set up of automatically updating cross reference. lukebrown Word VBA 5 09-11-2013 09:38 AM
How to insert a cross reference nested in a TC field and automatically get the correct autonumber? Cross-reference together with Mathtype formula is not working correct Santa_Clause Word 2 02-02-2010 04:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:35 PM.


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