Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Mar 2010
    Location
    Los Angeles, CA, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word 2007 VBA GetCrossReferenceItems(wdRefTypeHeading) returns truncated variant array

    I have a macro that uses ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding) to return all xref'ed items in the document. I get a full list but notice the strings seem to be limited to ~ 95 characters, so I don't get the entire heading string. Is this a known limit of variant arrays? I can't find anything about this on the web.

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,498
    Thanks
    3
    Thanked 139 Times in 132 Posts
    I haven't heard of a limitation and would guess the limit of ~95 characters is more to do with what can be displayed in the Insert Cross-Reference dialog.

    But then, why are you looking at the list of cross-ref headings if you want the list of actual cross-references in the document?
    You can loop through all fields in the document to find all the cross-references. Looping through all the bookmarks will reveal where each cross-reference is pointing.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #3
    New Lounger
    Join Date
    Mar 2010
    Location
    Los Angeles, CA, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply. I'm using a loop to look up the index number in order to insert a heading cross reference based on a string (lstring). This is just a portion of a macro to replace [xref:XXXX] with the proper xref from DOORS database.

    I noticed in debug mode that MyHeadings array truncated some of the longer heading strings. Here's an example MyHeading(1) that I modified to find out the limitation (original is "Purpose and Scope567890123456789012345678901234567890123456789 01234567890123456789012345678901234567890"). Note the string truncates @ 96 characters.

    MyHeading(1) = " 1.1 Purpose and Scope567890123456789012345678901234567890123456789 012345678901234567890123456"

    MyHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)
    For i = 1 To UBound(MyHeadings)
    A = Trim(LCase$(MyHeadings(i)))
    B = Left(LCase$(lstring), Len(A))
    If InStr(A, B) Then
    Exit For
    End If
    Next i
    If i > UBound(MyHeadings) Then
    MsgBox "There is an error searching for headings match: " & lstring & vbLf, vbInformation
    End If

    ' insert heading number xref
    Selection.InsertCrossReference _
    ReferenceType:=wdRefTypeHeading, _
    ReferenceKind:=wdNumberFullContext, _
    ReferenceItem:=i, _
    InsertAsHyperlink:=True

    ' insert heading text xref
    Selection.TypeText (" ") 'space first
    Selection.InsertCrossReference _
    ReferenceType:=wdRefTypeHeading, _
    ReferenceKind:=wdContentText, _
    ReferenceItem:=i, _
    InsertAsHyperlink:=True
    Last edited by Aaron451; 2014-01-10 at 17:25.

  5. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,498
    Thanks
    3
    Thanked 139 Times in 132 Posts
    I just tested on my Word 2010 and get the same truncation when using the GetCrossReferenceItems to populate the array.

    Interestingly, the string array is not the culprit since I can do this without getting entries truncated at ~96 chars
    Code:
    Sub Test()
      Dim MyHeadings() As String
      Dim i As Integer  
      'MyHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
      MyHeadings = Split(ActiveDocument.Content.Text, vbCr)
      For i = 1 To UBound(MyHeadings)
        Debug.Print MyHeadings(i)
      Next i
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #5
    New Lounger
    Join Date
    Mar 2010
    Location
    Los Angeles, CA, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for taking the time to verify my findings. I can't believe no one has run into this in the past and can't find anything from Microsoft or searching the web on this limitation. Guess I'll have to throw this at MS.

    Do you know of a different method to get all the headings?
    Last edited by Aaron451; 2014-01-13 at 10:57.

  7. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,498
    Thanks
    3
    Thanked 139 Times in 132 Posts
    I don't know of a vba collection to do this so I would do it by a workaround

    Eg. Add a TOC to the start of the document and harvest the paragraphs to fill an array and then remove the TOC. This has the side benefit of easily assigning the level as a second dimension to the array.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Tags for this Thread

Posting Permissions

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