View Single Post
 
Old 04-23-2014, 03:46 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

If, as advised in my previous reply, you start off with all of the footnotes & endnotes at the end of the document and all the footnote & endnote references as cross-references to those endnotes, running the following macro after your editing is done will ensure all footnote & endnote references are placed before any applicable cross-references. You will then only need to delete whatever footnote & endnote references are still at the end of the document after running the macro.
Code:
Sub ReorderNotes()
Application.ScreenUpdating = False
Dim Fld As Field, NtRng As Range, FldRng As Range
Dim Rng As Range, StrOld As String, StrNew As String
Dim ENtStyle As Long, FNtStyle As Long, i As Long, j As Long
With ActiveDocument
  ENtStyle = .Endnotes.NumberStyle
  FNtStyle = .Footnotes.NumberStyle
  .Endnotes.NumberStyle = wdNoteNumberStyleArabic
  .Footnotes.NumberStyle = wdNoteNumberStyleArabic
  .Fields.Update
  For Each Fld In .Fields
    If Fld.Type = wdFieldNoteRef Then
      Set FldRng = Fld.Code
      i = Fld.Result
      While FldRng.Fields.Count = 0
        FldRng.End = FldRng.End + 1
      Wend
      Set NtRng = .Endnotes(i).Reference
      If NtRng.End > FldRng.End Then
        StrOld = FldRng.Fields(1).Code.Text
        FldRng.Fields(1).Delete
        NtRng.Cut
        FldRng.Paste
        FldRng.Style = "EndNote Reference"
        j = FldRng.Endnotes(1).Index
        NtRng.InsertCrossReference ReferenceType:="Endnote", ReferenceKind:= _
          wdEndnoteNumberFormatted, ReferenceItem:=j, InsertAsHyperlink:=True
        If FldRng.Characters.First = " " Then FldRng.Characters.First = vbNullString
        StrNew = NtRng.Words.First.Fields(1).Code.Text
        ActiveWindow.View.ShowFieldCodes = True
        For Each Rng In .StoryRanges
          With Rng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = StrOld
            .Replacement.Text = StrNew
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = True
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
          End With
          Rng.Fields.Update
        Next
        ActiveWindow.View.ShowFieldCodes = False
      End If
    ElseIf Fld.Type = wdFieldFootnoteRef Then
      Set FldRng = Fld.Code
      i = Fld.Result
      While FldRng.Fields.Count = 0
        FldRng.End = FldRng.End + 1
      Wend
      Set NtRng = .Footnotes(i).Reference
      If NtRng.End > FldRng.End Then
        StrOld = FldRng.Fields(1).Code.Text
        FldRng.Fields(1).Delete
        NtRng.Cut
        FldRng.Paste
        FldRng.Style = "FootNote Reference"
        j = FldRng.Endnotes(1).Index
        NtRng.InsertCrossReference ReferenceType:="Footnote", ReferenceKind:= _
          wdFootnoteNumberFormatted, ReferenceItem:=j, InsertAsHyperlink:=True
        If FldRng.Characters.First = " " Then FldRng.Characters.First = vbNullString
        StrNew = NtRng.Words.First.Fields(1).Code.Text
        ActiveWindow.View.ShowFieldCodes = True
        For Each Rng In .StoryRanges
          With Rng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = StrOld
            .Replacement.Text = StrNew
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = True
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
          End With
          Rng.Fields.Update
        Next
        ActiveWindow.View.ShowFieldCodes = False
      End If
    End If
  Next
  .Endnotes.NumberStyle = ENtStyle
  .Footnotes.NumberStyle = FNtStyle
  .Fields.Update
End With
Application.ScreenUpdating = True
End Sub
Note: Although you've only mentioned endnotes, the code is written to process footnotes as well. If you only have one or the other, the difference in execution time is negligible.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote