Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Aug 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add page references automatically (English/Word/2000)

    Complete code idiot here, throwing myself on your mercies...

    I need to write a Word marco that will take the entries in the headings collection, search the text of the document for the text in those headings NOT in a heading style (ie, in Normal or Hyperlink or such), and replace the text with a cross reference to the text in format of "XXX on page x".

    I've taken a couple of stabs at it, but I need a little hand holding, as I'm getting nowhere fast.

    Here's my attempts.....
    -------------------------------------------
    Dim CurrentHeading As Variable
    Sub FixCrossRefs()
    myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)
    Set myRange = ActiveDocument.Range(Start:=ActiveDocument.Content .Start, End:=ActiveDocument.Content.End)


    For i = 1 To UBound(myHeadings) - 1


    With myRange.Find
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute FindText:=myHeadings(i)
    End With


    With myRange
    .InsertCrossReference ReferenceType:=wdRefTypeHeading, ReferenceKind:=wdContentText, ReferenceItem:=i
    .InsertAfter " on page "
    .Collapse Direction:=wdCollapseEnd
    .InsertCrossReference ReferenceType:=wdRefTypeHeading, ReferenceKind:=wdPageNumber, ReferenceItem:=i
    .Collapse Direction:=wdCollapseEnd
    .InsertParagraphAfter
    End With

    Next
    End Sub


    Sub FixCrossRefsVer2()

    myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)
    Set myRange = ActiveDocument.Content

    For i = 1 To UBound(myHeadings) - 1
    With myRange.Find
    .Execute FindText:=myHeadings(i), Forward:=True

    End With

    With myRange
    .Collapse Direction:=wdCollapseEnd
    .InsertCrossReference ReferenceType:=wdRefTypeHeading, ReferenceKind:=wdContentText, ReferenceItem:=i
    .InsertAfter " on page "
    .Collapse Direction:=wdCollapseEnd
    .InsertCrossReference ReferenceType:=wdRefTypeHeading, ReferenceKind:=wdPageNumber, ReferenceItem:=i
    End With


    Next i
    End Sub
    ----------------------

    I appreciate whatever help you can give!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Add page references automatically (English/Word/2000)

    Welcome to Woody's Lounge!

    Try this - it's slightly less elegant, since it uses Selection, but it works in a test I did.

    Sub FixCrossRefs()
    Dim myHeadings
    Dim i As Long
    Dim f As Boolean

    Application.ScreenUpdating = False

    myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)
    For i = 1 To UBound(myHeadings)
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .Forward = True
    .ClearFormatting
    .Text = Trim(myHeadings(i))
    .MatchWholeWord = True
    .MatchCase = False
    .Wrap = wdFindStop
    Do
    f = .Execute
    If f = False Then Exit Do
    If Left(Selection.Style, 3) <> "Heading" Then
    Selection.InsertCrossReference _
    ReferenceType:=wdRefTypeHeading, _
    ReferenceKind:=wdContentText, ReferenceItem:=i
    Selection.InsertAfter " on page "
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.InsertCrossReference _
    ReferenceType:=wdRefTypeHeading, _
    ReferenceKind:=wdPageNumber, ReferenceItem:=i
    Selection.InsertParagraphAfter
    Selection.Collapse Direction:=wdCollapseEnd
    End If
    Loop
    End With
    Next i

    Application.ScreenUpdating = True
    End Sub

  3. #3
    New Lounger
    Join Date
    Aug 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add page references automatically (English/Wor

    Fantastic! I had to tweak a bit to get this to where I needed, but I couldn't have gotten there without the help.

    For reference (and the benefit of others like me using Google), this script lets me automatically add cross reference page numbers to the Word output from Macromedia RoboHelp.

    Thanks again!

Posting Permissions

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