View Single Post
 
Old 01-12-2023, 10:32 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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

The first two problems you have are:
1. wdRefTypeCustomHeading doesn't exist
2. Selection.GoTo doesn't have a type that correlates with wdRefTypeNumberedItem

Try this version of what I think you are trying to do.
Code:
Sub GoToHeading_test2()
  Dim sArticleNum As String
   
  sArticleNum = Trim(LCase(InputBox("Enter the article number", "Enter article number")))
  
  If SetRef(wdRefTypeHeading, sArticleNum) Then Exit Sub
  If SetRef(wdRefTypeNumberedItem, sArticleNum) Then Exit Sub
  'If you get this far the clause number wasn't found
  MsgBox "Couldn't find the heading containing: " & sArticleNum
End Sub

Function SetRef(iType As Integer, sParaNum As String) As Boolean
  Dim vArray As Variant, i As Integer, v As String, vLower As String
  Dim sCode As String, aXRef As Field, aRng As Range
  vArray = ActiveDocument.GetCrossReferenceItems(iType)
  For i = LBound(vArray) To UBound(vArray)
    vLower = Trim(LCase(vArray(i)))
    Debug.Print sParaNum, vLower
    If vLower Like sParaNum & "*" Then
      Set aRng = ActiveDocument.Range(Start:=0, End:=0)
      aRng.InsertCrossReference ReferenceType:=iType, ReferenceKind:=wdContentText, ReferenceItem:=i, InsertAsHyperlink:=True
      Set aXRef = ActiveDocument.Fields(1)
      Debug.Print aXRef.Code
      sCode = Split(Trim(aXRef.Code), " ")(1)
      Selection.GoTo What:=wdGoToBookmark, Name:=sCode
      aXRef.Delete
      SetRef = True
      Exit For
    End If
  Next i
End Function
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote