+ Reply to Thread
Results 1 to 2 of 2

Get a cross reference item number based on heading text

  1. #1
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Get a cross reference item number based on heading text

    Hi all,

    I have a ton of documents with various calls to action (i.e. Go to Section 1.21.)

    At these locations I need to insert cross references (i.e. 1.21. MyHeading ABC)

    I can do this manually - e.g. go to cross references and insert the heading number and heading text. However, I need to do this programatically. The code for doing this is:

    Please Login or Register  to view this content.
    This works just fine if I know the ReferenceItem, except I don't.

    I can loop through each heading paragraph to returns the full paragraph heading based on my selected number (1.21.)

    But how can I use this to get a ReferenceItem number from a paragraph loop?

    Would really appreciate any ideas.

    Cheers

  2. #2
    Registered User
    Join Date
    11-06-2007
    Posts
    28

    Re: Get a cross reference item number based on heading text

    I was looking for answer to same question when I found this thread; there's no answer here... but it gave me inspiration for solving the issue! :-)

    This maro parses all Cross Reference Items of type "wdRefTypeHeading" and compare their paragraph number with number of currently selected paragraph: if they match, text insertion point is moved one line above, and this text is added:

    §xxx, p.yyy

    Where xxx and yyy are of course paragraph number and page number.

    Then the text is cut and put in clipboard, so original document is not affected.

    **NOTE: an empty line must be present before the heading, else any contents of above line will be cut.**

    Sub CreaRiferimento()
    Dim d As Document
    Set d = ActiveDocument
    Debug.Print "Current selection: " & Selection.Paragraphs(1).Range.ListFormat.ListString & " " & Selection.Paragraphs(1).Range
    n = GetRefNum()
    Debug.Print "Internal reference number = " & n

    ' Sposta punto inserimento sopra a titolo
    Selection.MoveUp Unit:=wdLine, Count:=1

    ' Scrive "§"
    Selection.TypeText Text:="§"

    ' Scrive riferimento a paragrafo
    Selection.InsertCrossReference ReferenceType:="Titolo", ReferenceKind:= _
    wdNumberRelativeContext, ReferenceItem:=Trim(Str(n)), InsertAsHyperlink:=True, _
    IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

    ' Scrive "p." per la pagina
    Selection.TypeText Text:=", p."

    'Scrive riferimento a pagina del paragrafo
    Selection.InsertCrossReference ReferenceType:="Titolo", ReferenceKind:= _
    wdPageNumber, ReferenceItem:=Trim(Str(n)), InsertAsHyperlink:=True, _
    IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

    ' Taglia in clipboard quanto appena scritto
    Selection.EndKey Unit:=wdLine
    Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
    Selection.Cut

    End Sub

    Function GetRefNum() As String
    CRI = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
    HeadNum = Selection.Paragraphs(1).Range.ListFormat.ListString
    CRINumber = 1
    For Each CR In CRI
    HeadingNumberFromFunction = Mid$(LTrim(CR), 1, InStr(LTrim(CR), " ") - 1)
    'Debug.Print HeadNum & " ? " & HeadingNumberFromFunction
    'Debug.Print "'" & CR & " ' = ReferenceItem n." & CRINumber
    If HeadingNumberFromFunction = HeadNum Then
    GetRefNum = CRINumber
    Exit Function
    End If
    CRINumber = CRINumber + 1
    Next

    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Data Cross Reference (validation) based on Text in a Cell
    By kwjoh in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-06-2015, 01:57 AM
  2. Cross Reference Item List and Relating Data
    By pynergee in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-28-2013, 03:28 PM
  3. Replies: 3
    Last Post: 03-12-2013, 05:31 PM
  4. Cross Reference item from one colum to items in a new column
    By mbrackey in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-07-2013, 03:36 PM
  5. [SOLVED] Cross Reference Lists to find Duplicates & List Line Item
    By mycon73 in forum Excel General
    Replies: 3
    Last Post: 01-01-2013, 07:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1